2。从数据库中读取图片文件
// 从数据库中读取图片,并显示在PictureBoc控件中
private void button2_Click( object sender, EventArgs e)
{
int id = 6 ; // 要读取的图片在数据库中的编号,因为是测试就手动修改要读取的编号
try
{
OleDbConnection conn = new OleDbConnection(conStr);
string sql = $ " select pictText from mTable where ID={id} " ;
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
oda.Fill(dt);
conn.Close();
conn.Dispose();
if (dt.Rows.Count> 0 )
{
pic = dt.Rows[ 0 ][ 0 ].ToString();
if (! string .IsNullOrEmpty(pic))
{
// 把string转成字节数组
byte [] imageBytes = Convert.FromBase64String(pic);
// 把字节读取到内存
MemoryStream memoryStream = new MemoryStream(imageBytes, 0 , imageBytes.Length);
memoryStream.Write(imageBytes, 0 , imageBytes.Length);
// 字节数组转成Image对象
Image image = Image.FromStream(memoryStream);
// Image image = StringToImage(pic);
// 显示图片到控件中
this .pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
this .pictureBox2.Image = image;
}
}
}
catch (Exception)
{
throw ;
}
}
学习成果展示:
好了今天就学到这了。
C#读写图片文件到Access数据库中
标签:orm ros insert gre rom byte isp sage span
查看更多关于C#读写图片文件到Access数据库中的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did116725