C#操作圖片讀取和存儲(chǔ)SQLserver實(shí)現(xiàn)代碼
更新時(shí)間:2013年03月07日 11:47:41 作者:
用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫(kù)/將圖片數(shù)據(jù)從SQLserver中取出來(lái)并顯示到pictureBox控件上,接下來(lái)將為你詳細(xì)介紹下實(shí)現(xiàn)步驟,感興趣的你可以參考下
一、用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫(kù):
1.1 將圖片控件的Image轉(zhuǎn)換成流:
private byte[] PicToArray()
{
Bitmap bm = new Bitmap(picBox.Image);
MemoryStream ms = new MemoryStream();
bm.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
//保存到數(shù)據(jù)庫(kù)
try
{
string sql = "update T_Employee set ImageLogo=@ImageLogo where EmpId=@EmpId";
SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@ImageLogo", imgSourse));
MessageBox.Show("修改已保存!");// ShowInfo(0);
}
catch (Exception ex)
{
MessageBox.Show("更新失??!" + ex.Message);
return;
}
1.2將圖片文件轉(zhuǎn)換成字節(jié)流并插入數(shù)據(jù)庫(kù):
class ImageInserter
{
public static int InsertImg(string path)
{
//----------以文件的方式讀取圖片并轉(zhuǎn)化成字節(jié)流
FileStream fs = new FileStream(path,FileMode.Open);
byte[] imgSourse = new byte[fs.Length];
fs.Read(imgSourse,0,imgSourse.Length);
fs.Close();
using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update T_Employee set ImageLogo=@ImageLogo";
// cmd.Parameters.Add("@ImageLogo", SqlDbType.Image);
cmd.Parameters.Add(new SqlParameter("@ImageLogo", imgSourse));
return cmd.ExecuteNonQuery();
}
}
}
二、將圖片數(shù)據(jù)從SQLserver中取出來(lái)并顯示到pictureBox控件上:
byte[] ImageLogoArray = row["ImageLogo"] is DBNull ? null :(byte[])(row["ImageLogo"]);
MemoryStream ms=null;
if (ImageLogoArray!=null)
{
ms = new MemoryStream(ImageLogoArray);
picBox.Image = new Bitmap(ms);
}
1.1 將圖片控件的Image轉(zhuǎn)換成流:
復(fù)制代碼 代碼如下:
private byte[] PicToArray()
{
Bitmap bm = new Bitmap(picBox.Image);
MemoryStream ms = new MemoryStream();
bm.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
復(fù)制代碼 代碼如下:
//保存到數(shù)據(jù)庫(kù)
try
{
string sql = "update T_Employee set ImageLogo=@ImageLogo where EmpId=@EmpId";
SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@ImageLogo", imgSourse));
MessageBox.Show("修改已保存!");// ShowInfo(0);
}
catch (Exception ex)
{
MessageBox.Show("更新失??!" + ex.Message);
return;
}
1.2將圖片文件轉(zhuǎn)換成字節(jié)流并插入數(shù)據(jù)庫(kù):
復(fù)制代碼 代碼如下:
class ImageInserter
{
public static int InsertImg(string path)
{
//----------以文件的方式讀取圖片并轉(zhuǎn)化成字節(jié)流
FileStream fs = new FileStream(path,FileMode.Open);
byte[] imgSourse = new byte[fs.Length];
fs.Read(imgSourse,0,imgSourse.Length);
fs.Close();
using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update T_Employee set ImageLogo=@ImageLogo";
// cmd.Parameters.Add("@ImageLogo", SqlDbType.Image);
cmd.Parameters.Add(new SqlParameter("@ImageLogo", imgSourse));
return cmd.ExecuteNonQuery();
}
}
}
二、將圖片數(shù)據(jù)從SQLserver中取出來(lái)并顯示到pictureBox控件上:
復(fù)制代碼 代碼如下:
byte[] ImageLogoArray = row["ImageLogo"] is DBNull ? null :(byte[])(row["ImageLogo"]);
MemoryStream ms=null;
if (ImageLogoArray!=null)
{
ms = new MemoryStream(ImageLogoArray);
picBox.Image = new Bitmap(ms);
}
相關(guān)文章
C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果
本文主要介紹了C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01輸出的文本實(shí)現(xiàn)對(duì)齊的方法(超簡(jiǎn)單)
下面小編就為大家分享一篇c#輸出的文本實(shí)現(xiàn)對(duì)齊的方法,特別簡(jiǎn)單!希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12vista和win7在windows服務(wù)中交互桌面權(quán)限問(wèn)題解決方法:穿透Session 0 隔離
服務(wù)(Service)對(duì)于大家來(lái)說(shuō)一定不會(huì)陌生,它是Windows 操作系統(tǒng)重要的組成部分。我們可以把服務(wù)想像成一種特殊的應(yīng)用程序,它隨系統(tǒng)的“開(kāi)啟~關(guān)閉”而“開(kāi)始~停止”其工作內(nèi)容,在這期間無(wú)需任何用戶參與2016-04-04C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類介紹
本文主要介紹了C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類的基礎(chǔ)的使用,并做了相關(guān)的代碼演示,供初學(xué)者參考。2016-03-03基于Unity實(shí)現(xiàn)2D邊緣檢測(cè)
這篇文章主要介紹了如何利用Unity實(shí)現(xiàn)2D邊緣檢測(cè),從而達(dá)到人物描邊效果。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04