二进制转图片错误

sunhaobin123 2010-09-23 06:01:30
我在从数据库中读取二进制图片的时候有点问题,报异常为参数无效,很郁闷。。。其中bytes检查过是有数据的
问题比较紧急~在线等


string sql = " select faceImage from faceImage";
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
try
{
DBHelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
byte[] bytes = null;
while (dataReader.Read())
{
bytes = (byte[])dataReader[0];
imglistFace.Images.Add(Image.FromStream(new MemoryStream(bytes)));
MessageBox.Show(imglistFace.Images.Count.ToString());
}
dataReader.Close();
...全文
243 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunhaobin123 2010-09-23
  • 打赏
  • 举报
回复
添加是成功的啊,在数据库中查询,得到的是0x53797374656D2E427974655B5D
wuyq11 2010-09-23
  • 打赏
  • 举报
回复
添加时是否成功
byte[] image = (byte[])(dt.Rows[0]["photo"]);
if (image.Length > 20)
{
Response.Write(System.BitConverter.ToString(image, 0, 20));
}

保存到数据库中的是:53-79-73-74-65-6D-2E-42-79-74-65-5B-5D就是Systme.Byte[] 字符串
sunhaobin123 2010-09-23
  • 打赏
  • 举报
回复
我将dataReader的读取内容获取了出来,并不是0x53797374656D2E427974655B5D这样的数据。而是system.byte[],这是为什么啊,数据库里面明明存放的数据,为什么读的不是数据啊?
sunhaobin123 2010-09-23
  • 打赏
  • 举报
回复

数据库中的数据是这样的,是不是和这个有关系?
0x53797374656D2E427974655B5D
sunhaobin123 2010-09-23
  • 打赏
  • 举报
回复
byte[] bytes = null;
while (dataReader.Read())
{
bytes = (byte[])dataReader[0];
if (bytes.Length > 0)
{
MemoryStream ms = new MemoryStream(bytes, true);
ms.Write(bytes, 0, bytes.Length);
Bitmap bmp = new Bitmap(ms);
//imglistFace.Images.Add(bmp);
//MessageBox.Show(imglistFace.Images.Count.ToString());
}
}
这样改后,还是一致报出参数无效啊~~
wuyq11 2010-09-23
  • 打赏
  • 举报
回复
确保没有无效数据。
检查字段长度
byte[] b= (byte[])dataReader[0];
if (b.Length>0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}

111,129

社区成员

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

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

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