怎样读取PictureBox中的数据

yishuihepan009 2010-10-05 04:52:42
怎样读取PictureBox中的数据,并存储进数据库中?
求个实例或是源代码。
...全文
116 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yishuihepan009 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ki1381 的回复:]
操作数据库的代码本质上是一样的,都是转成byte[]然后保存。要说有不同,那就是ConnectionString和Access的保存图片的类型会不同,要using的命名空间也有点小不同。
[/Quote]

能详细介绍下吗?
Ki1381 2010-10-06
  • 打赏
  • 举报
回复
操作数据库的代码本质上是一样的,都是转成byte[]然后保存。要说有不同,那就是ConnectionString和Access的保存图片的类型会不同,要using的命名空间也有点小不同。
yishuihepan009 2010-10-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Position = 0;
byte[] imageBytes = new byte[ms.Length];
ms.Read(imageBytes,0,imageBytes.……
[/Quote]

大家给的都是将数据存入SQLSERVER的,可是本人用的是access啊。应该怎样存入?
水哥阿乐 2010-10-05
  • 打赏
  • 举报
回复
按二进制读取
//按二进制流的形式保存到数据库
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = ms.ToArray();
//从数据库内读出的数据
byte[] imageBytes = new byte[10];
pictureBox1.Image = Bitmap.FromStream(new System.IO.MemoryStream(imageBytes));
caojinrong 2010-10-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Position = 0;
byte[] imageBytes = new byte[ms.Length];
ms.Read(imageBytes,0,imageBytes.……
[/Quote]

思路完全正确,细节上可以微调一下

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
//ms.Position = 0;
//byte[] imageBytes = new byte[ms.Length];
//ms.Read(imageBytes,0,imageBytes.Length);

cmd.Parameters.Add("@img", SqlDbType.Image, (int)ms.Length);
cmd.Parameters["@img"].Value = ms.ToArray();
cmd.ExecuteNonQuery();

ms.Close();
无乐不作111 2010-10-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Position = 0;
byte[] imageBytes = new byte[ms.Length];
ms.Read(imageBytes,0,imageBytes……
[/Quote]
直接保存二进制数据
比较简洁了已经
zhoujk 2010-10-05
  • 打赏
  • 举报
回复
到我的空间去下一个 位图与数组的转换函数来试一下
wuyq11 2010-10-05
  • 打赏
  • 举报
回复
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Position = 0;
byte[] imageBytes = new byte[ms.Length];
ms.Read(imageBytes,0,imageBytes.Length);

cmd.Parameters.Add("@img", SqlDbType.Image, (int)ms.Length);
cmd.Parameters["@img"].Value = imageBytes ;
cmd.ExecuteNonQuery();

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧