C#实现图片的保存读取
C#实现把pictureBox控件的image图片保存到sql数据库,把sql数据库的image数据显示到pictureBox控件上,尽量要源代码...
C#实现把pictureBox控件的image图片保存到sql数据库,把sql数据库的image数据显示到pictureBox控件上,尽量要源代码
展开
展开全部
数据库存储图片,字段数据类型是image
Bitmap jpg = new Bitmap(this.pictureBox1.Image);
Graphics draw = Graphics.FromImage(jpg);
((Image)jpg).Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
if (ms.Length > 0)
{
byte[] mywave = new byte[ms.Length];
mywave = ms.ToArray();
ms.Close();
SqlConnection conn =""://数据库字符串
SqlCommand cmd = conn.CreateCommand();
string sqltext = "insert into table (image)values (@img) )";//简写的SQL语句,只是告诉你图片字段对应@img
cmd.CommandText = sqltext;
cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = mywave;
}
显示图片:
先将数据查出来
getds.Tables[0].Rows[0]["image"]对应的是图片数据,getds是查询得到的dataset
ms = new MemoryStream((byte[])getds.Tables[0].Rows[0]["image"]);
Image i = Image.FromStream(ms, true);
pictureBox1.Image = i;
Bitmap jpg = new Bitmap(this.pictureBox1.Image);
Graphics draw = Graphics.FromImage(jpg);
((Image)jpg).Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
if (ms.Length > 0)
{
byte[] mywave = new byte[ms.Length];
mywave = ms.ToArray();
ms.Close();
SqlConnection conn =""://数据库字符串
SqlCommand cmd = conn.CreateCommand();
string sqltext = "insert into table (image)values (@img) )";//简写的SQL语句,只是告诉你图片字段对应@img
cmd.CommandText = sqltext;
cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = mywave;
}
显示图片:
先将数据查出来
getds.Tables[0].Rows[0]["image"]对应的是图片数据,getds是查询得到的dataset
ms = new MemoryStream((byte[])getds.Tables[0].Rows[0]["image"]);
Image i = Image.FromStream(ms, true);
pictureBox1.Image = i;
更多追问追答
追问
里面的ms的定义是什么
追答
MemoryStream ms = new MemoryStream();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询