C#從數(shù)據(jù)庫讀取圖片并保存的兩種方法
方式一:
數(shù)據(jù)庫用的是SQL 2008,數(shù)據(jù)表中存放的是圖片的二進制數(shù)據(jù),現(xiàn)在把圖片以一種圖片格式(如.jpg)導出,然后存放于指定的文件夾中,實現(xiàn)方式如下:
byte[] bytImg = (byte[])myDAL.DbHelperSQL.Query("SELECT F_Photo FROM myTable WHERE ID=1").Tables[0].Rows[0][0]; if (bytImg != null) { MemoryStream ms = new MemoryStream(bytImg); Image img = Image.FromStream(ms); img.Save("D:\\me.jpg"); }
方式二:
是windowform程序,數(shù)據(jù)庫已經(jīng)建好,圖像以二進制形式存放在數(shù)據(jù)庫的image表中,我想把符合查詢條件的圖像(大量)從數(shù)據(jù)庫中讀出,顯示在form窗體上的一個控件(listview或imagelist還是picturebox?這個不知道那個合適),并保存到選擇(或新建)的一個文件夾中
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);//數(shù)據(jù)庫連接,修改一下數(shù)據(jù)庫的操作。 DataSet ds = new DataSet(); da.Fill(ds, "pic");//將符合條件的選項保存在數(shù)據(jù)集的pic表里 string picdotname; string picfilename; int piclength; int i; //添加新列 DataColumn newcolumn = ds.Tables["pic"].Columns.Add("pic_url", typeof(string));//給pic表添加新的一列pic_url,保存你的新寫出的圖片路徑 for (i = 0; i < Convert.ToInt16(ds.Tables["pic"].Rows.Count); i++) { picdotname = ds.Tables["pic"].Rows[i]["pic_dot"].ToString();//圖片的拓展名,你數(shù)據(jù)庫要有這一列,如jpg piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);//數(shù)據(jù)流的長度 picfilename = Server.MapPath("新建的文件夾名/") + "添加圖片名"+ "." + picdotname; FileStream fs = new FileStream(picfilename, FileMode.Create, FileAccess.Write); byte[] piccontent = new byte[piclength]; piccontent = (byte[])ds.Tables["pic"].Rows[i]["pic_content"]; fs.Write(piccontent, 0, piclength); fs.Close();//讀出數(shù)據(jù)流寫成圖片 //最后把表綁定到控件上。 ds.Tables["pic"].Rows[i]["pic_url"] = "temp/temp" + i.ToString() + "." + picdotname;//意思給表pic的第i行,pic_url列里添加文件的路徑值。 } //數(shù)據(jù)源 = ds.Tables["pic"];//數(shù)據(jù)綁定
大體是這樣吧,里面表名列名很多細節(jié)你按你的表修改吧!
以上就是C#從數(shù)據(jù)庫讀取圖片并保存的兩種方法的詳細內容,更多關于c# 讀取圖片并保存的資料請關注腳本之家其它相關文章!
- C#連接SQL數(shù)據(jù)庫和查詢數(shù)據(jù)功能的操作技巧
- C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例
- C# 創(chuàng)建MDB數(shù)據(jù)庫、并存放表格數(shù)據(jù)的案例
- C#使用SqlServer作為日志數(shù)據(jù)庫的設計與實現(xiàn)
- C#窗體-數(shù)據(jù)庫連接及登錄功能的實現(xiàn)案例
- C#連接SQL Server數(shù)據(jù)庫的實例講解
- C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
- C# Ado.net實現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲過程列表及參數(shù)信息示例
- c#使用FreeSql生產環(huán)境時自動升級備份數(shù)據(jù)庫
相關文章
C#使用OpenCvSharp4庫讀取電腦攝像頭數(shù)據(jù)并實時顯示
OpenCvSharp4庫是一個基于.Net封裝的OpenCV庫,本文主要給大家介紹了C#使用OpenCvSharp4庫讀取電腦攝像頭數(shù)據(jù)并實時顯示的詳細方法,感興趣的朋友可以參考下2024-01-01