C#操作圖片讀取和存儲(chǔ)SQLserver實(shí)現(xiàn)代碼
更新時(shí)間:2013年03月07日 11:47:41 作者:
用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫(kù)/將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上,接下來將為你詳細(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中取出來并顯示到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中取出來并顯示到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# HttpClient Cookie驗(yàn)證解決方法
本文將詳細(xì)介紹C# HttpClient Cookie驗(yàn)證解決方法,需要了解的朋友可以參考下2012-11-11
C# 設(shè)計(jì)模式系列教程-簡(jiǎn)單工廠模式
簡(jiǎn)單工廠模式職責(zé)單一,實(shí)現(xiàn)簡(jiǎn)單,且實(shí)現(xiàn)了客戶端代碼與具體實(shí)現(xiàn)的解耦。2016-06-06
C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條顯示效果,感興趣的小伙伴可以嘗試一下2022-06-06

