C#從數(shù)據(jù)庫(kù)讀取圖片并保存的兩種方法
方式一:
數(shù)據(jù)庫(kù)用的是SQL 2008,數(shù)據(jù)表中存放的是圖片的二進(jìn)制數(shù)據(jù),現(xiàn)在把圖片以一種圖片格式(如.jpg)導(dǎo)出,然后存放于指定的文件夾中,實(shí)現(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ù)庫(kù)已經(jīng)建好,圖像以二進(jìn)制形式存放在數(shù)據(jù)庫(kù)的image表中,我想把符合查詢條件的圖像(大量)從數(shù)據(jù)庫(kù)中讀出,顯示在form窗體上的一個(gè)控件(listview或imagelist還是picturebox?這個(gè)不知道那個(gè)合適),并保存到選擇(或新建)的一個(gè)文件夾中
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);//數(shù)據(jù)庫(kù)連接,修改一下數(shù)據(jù)庫(kù)的操作。 DataSet ds = new DataSet(); da.Fill(ds, "pic");//將符合條件的選項(xiàng)保存在數(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ù)庫(kù)要有這一列,如jpg piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);//數(shù)據(jù)流的長(zhǎng)度 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ù)綁定
大體是這樣吧,里面表名列名很多細(xì)節(jié)你按你的表修改吧!
以上就是C#從數(shù)據(jù)庫(kù)讀取圖片并保存的兩種方法的詳細(xì)內(nèi)容,更多關(guān)于c# 讀取圖片并保存的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C#連接SQL數(shù)據(jù)庫(kù)和查詢數(shù)據(jù)功能的操作技巧
- C# TreeView從數(shù)據(jù)庫(kù)綁定數(shù)據(jù)的示例
- C# 創(chuàng)建MDB數(shù)據(jù)庫(kù)、并存放表格數(shù)據(jù)的案例
- C#使用SqlServer作為日志數(shù)據(jù)庫(kù)的設(shè)計(jì)與實(shí)現(xiàn)
- C#窗體-數(shù)據(jù)庫(kù)連接及登錄功能的實(shí)現(xiàn)案例
- C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
- C#連接Oracle數(shù)據(jù)庫(kù)字符串(引入DLL)的方式
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例
- c#使用FreeSql生產(chǎn)環(huán)境時(shí)自動(dòng)升級(jí)備份數(shù)據(jù)庫(kù)
相關(guān)文章
C#使用OpenCvSharp4庫(kù)讀取電腦攝像頭數(shù)據(jù)并實(shí)時(shí)顯示
OpenCvSharp4庫(kù)是一個(gè)基于.Net封裝的OpenCV庫(kù),本文主要給大家介紹了C#使用OpenCvSharp4庫(kù)讀取電腦攝像頭數(shù)據(jù)并實(shí)時(shí)顯示的詳細(xì)方法,感興趣的朋友可以參考下2024-01-01Unity shader實(shí)現(xiàn)高斯模糊效果
這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)高斯模糊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#實(shí)現(xiàn)軟件開(kāi)機(jī)自動(dòng)啟動(dòng)的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)軟件開(kāi)機(jī)自動(dòng)啟動(dòng)的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07