首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

更新SQL SERVER 2005数据库里的图片字段的请问

2012-05-04 
更新SQL SERVER 2005数据库里的图片字段的请教我在程序里读取sql server 2005的image字段,显示在一个Pictu

更新SQL SERVER 2005数据库里的图片字段的请教
我在程序里读取sql server 2005的image字段,显示在一个PictureBox里,用户可以修改该PictureBox里的图片,然后保存。下面是我的代码:
读取图片并显示在PictureBox里,这段代码执行没有问题:

C# code
byte[] b = (byte[])dt.Rows[0]["Photo"];  //dt为查询出的DataTable,Photo为表里的image字段名称MemoryStream s = new MemoryStream(b, true);s.Write(b,0,b.Length);Image a = new Bitmap(s);Bitmap bit = new Bitmap(picPhoto.Width, picPhoto.Height);Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。g.DrawImage(a, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, a.Width, a.Height), GraphicsUnit.Pixel);picPhoto.Image = bit; 
//picPhoto为pictureBox 控件

下面是保存修改后的PictureBox里的图片到数据库的代码,这段代码出错,系统提示“将参数值从 Int32 转换到 Byte[] 失败”。:
C# code
MemoryStream ms = new MemoryStream();picPhoto.Image.Save("a.jpg");FileStream fs = new FileStream("a.jpg", FileMode.Open, FileAccess.Read);BinaryReader binaryReader = new BinaryReader(fs);byte[] img = binaryReader.ReadBytes((int)fs.Length);binaryReader.Close();fs.Close();File.Delete("a.jpg");info.Photo = img;     //info为定义的实体类


请问这是什么原因啊?

[解决办法]
byte[] img = binaryReader.ReadBytes((int)fs.Length);
调试是不是这里错了?ReadBytes()参数类型错了
[解决办法]
错误停在哪一句上?另外,文件到byte[]可以用File.ReadAllBytes完成,简单很多。

热点排行