日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c#form+mysql储存读取图片_C#从SQL server数据库中读取l图片和存入图片

發(fā)布時間:2023/12/10 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#form+mysql储存读取图片_C#从SQL server数据库中读取l图片和存入图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本實例主要介紹如何將圖片存入數(shù)據(jù)庫。將圖片存入數(shù)據(jù)庫,首先要在數(shù)據(jù)庫中建立一張表,將存儲圖片的字段類型設(shè)為Image類型,用FileStream類、BinaryReader把圖片讀成字節(jié)的形式,賦給一個字節(jié)數(shù)組,然后用ADO.SqlCommand對象的ExecuteNonQuery()方法來把數(shù)據(jù)保存到數(shù)據(jù)庫中。主要代碼如下:

private void button1_Click(object sender, EventArgs e)

{

openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

if(openFileDialog1.ShowDialog()==DialogResult.OK)

{

string fullpath =openFileDialog1.FileName;//文件路徑

FileStream fs = new FileStream(fullpath, FileMode.Open);

byte[] imagebytes =new byte[fs.Length];

BinaryReader br = new BinaryReader(fs);

imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

//打開數(shù)據(jù)庫

SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

con.Open();

SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

com.Parameters.Add("ImageList", SqlDbType.Image);

com.Parameters["ImageList"].Value = imagebytes;

com.ExecuteNonQuery();

con.Close();

}

}

本實例主要介紹如何從數(shù)據(jù)庫中把圖片讀出來。實現(xiàn)本實例主要是利用SqlDataReader從數(shù)據(jù)庫中把Image字段值讀出來,賦給一個byte[]字節(jié)數(shù)組,然后使用MemoryStream類與Bitmap把圖片讀取出來。主要代碼如下:

private void button1_Click(object sender, EventArgs e)

{

byte[] imagebytes = null;

//打開數(shù)據(jù)庫

SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

con.Open();

SqlCommand com = new SqlCommand("select top 1* from tb_09", con);

SqlDataReader dr = com.ExecuteReader();

while (dr.Read())

{

imagebytes = (byte[])dr.GetValue(1);

}

dr.Close();

com.Clone();

con.Close();

MemoryStream ms = new MemoryStream(imagebytes);

Bitmap bmpt = new Bitmap(ms);

pictureBox1.Image = bmpt;

}

本實例主要介紹如何只允許輸入指定圖片格式。用OpenFileDialog控件打開圖片文件,只要將OpenFileDialog控件的Filter屬性指定為特定的圖片格式即可。例如,打開.bmp文件的圖片,主要代碼如下:

this.openFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp";

在用pictureBox控件輸入圖片時,要想統(tǒng)一圖片大小,只需把控件的SizeMode屬性值設(shè)為StretchImage即可,StretchImage值表示圖像的大小將調(diào)整為控件的大小。這樣,圖片的大小就統(tǒng)一了。

總結(jié)

以上是生活随笔為你收集整理的c#form+mysql储存读取图片_C#从SQL server数据库中读取l图片和存入图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。