c#在sql中存取圖片image示例
(1)控制臺(tái)應(yīng)用程序下演示插入圖片
public void InsertIMG()
{
//將需要存儲(chǔ)的圖片讀取為數(shù)據(jù)流
FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
fs.Close();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = bt;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
(2)控制臺(tái)應(yīng)用程序下讀出并生成圖片到物理位置
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];//讀取第一個(gè)圖片的位流
int ArraySize= MyData.GetUpperBound(0);//獲得數(shù)據(jù)庫(kù)中存儲(chǔ)的位流數(shù)組的維度上限,用作讀取流的上限
FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //-- 寫入到c:\00.jpg。
conn.Close();
Console.WriteLine("讀取成功");//查看硬盤上的文件
}
}
(3)Web下picshow.aspx頁(yè)將圖片讀取出來并寫入到瀏覽器上呈現(xiàn)
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
Response.ContentType = "image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write("讀取成功");
}
(4)在web中可以如上picshow.aspx頁(yè)面讀取并顯示圖片,而真正引用該圖片時(shí)如下示例
<img src="picshow.aspx" width="500" height="300" />
(5)Winform下將圖片寫入到sql數(shù)據(jù)庫(kù)image類型字段中的方法和以上方法基本一致,僅區(qū)別于可以利用多個(gè)對(duì)話框來幫助選取存儲(chǔ)圖片等,各個(gè)屬性可以方便的利用上
(6)Winform下讀取圖片在picturebox控件中顯示出來
方法一:利用MemoryStream 和System.Drawing.Image
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
MemoryStream mystream = new MemoryStream(MyData);
//用指定的數(shù)據(jù)流來創(chuàng)建一個(gè)image圖片
System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
System.Windows.Forms.PictureBox picbox = new PictureBox();
picbox.Image = img;
picbox.Left = 30;
picbox.Top = 80;
picbox.Width = 800;
picbox.Height = 500;
this.Controls.Add(picbox);
mystream.Close();
conn.Close();
}
}
方法二:將流直接讀取成圖片并寫入到物理位置,然后再行利用該圖片呈現(xiàn)
void Read()
{
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
byte[] Image_img = (byte[])sdr["ImgFile"];
if (Image_img.Length == 0)
{
return;
}
int filelength = Image_img.Length;
string imageName = "1.jpg";
string myUrl = Environment.CurrentDirectory + "\\" + imageName;
FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter BW = new BinaryWriter(fs);
BW.BaseStream.Write(Image_img, 0, filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBox picbox = new PictureBox();
//為picbox添加圖片方法一
//picbox.ImageLocation = myUrl;
//picbox.Width = 800;
//picbox.Height = 300;
//為picbox添加圖片方法二
Bitmap bitmap = new Bitmap(myUrl);
picbox.Width = 100;//bitmap.Width;
picbox.Height = 80;//bitmap.Height;
picbox.Image = (Image)bitmap;
picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picbox.Left = 20;
picbox.Top = 30;
this.Controls.Add(picbox);
conn.Close();
}
}
相關(guān)文章
C#中的隊(duì)列Queue<T>與堆棧Stack<T>
這篇文章介紹了C#中的隊(duì)列Queue<T>與堆棧Stack<T>,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法示例總結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法,結(jié)合實(shí)例形式總結(jié)分析了C#通過反射、轉(zhuǎn)換等方法讀取匿名對(duì)象屬性值的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作
這篇文章主要介紹了C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
關(guān)于C#調(diào)用C++dll傳指針釋放內(nèi)存問題
這篇文章主要介紹了關(guān)于C#調(diào)用C++dll傳指針釋放內(nèi)存問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時(shí)會(huì)顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過派生ProfessionalColorTable類,下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08
c# FTP上傳文件實(shí)例代碼(簡(jiǎn)易版)
下面小編就為大家分享一篇c# FTP上傳文件的實(shí)例代碼,超簡(jiǎn)單哦~希望對(duì)大家有所幫助。一起跟隨小編過來看看吧,2017-12-12
C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝示例
這篇文章主要介紹了C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝,分析了微信網(wǎng)頁(yè)授權(quán)操作的原理、步驟并給出了C#實(shí)現(xiàn)的網(wǎng)頁(yè)授權(quán)操作邏輯封裝類,需要的朋友可以參考下2016-10-10
快速解決C# android base-64 字符數(shù)組的無效長(zhǎng)度問題
下面小編就為大家?guī)硪黄焖俳鉀QC# android base-64 字符數(shù)組的無效長(zhǎng)度問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08

