将数据库中的图像数据读取到图片框中时出错 [英] error in reading image data from database into a picture box

查看:63
本文介绍了将数据库中的图像数据读取到图片框中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




将图像类型数据导入图片框时出现问题
我已经创建了一个表格
tbl_items
具有属性




i have a problem in importing the image type data into a picture box
i have created a table
tbl_items
with attribute

item_image [image],<br />



我直接将图片保存到该属性
但我在将图像取回图片框时遇到问题

我的代码是



i saved the image directly to that attribute
but i have problem in retriving the image back to the picture box

my code is

public void Fload()
        {
            DataTable dt = Bllsqlhelper.getImagebyImageid(Convert.ToInt32(cmb_Jname.SelectedIndex));
            if (dt.Rows.Count > 0)
            {
               // if ((byte[])(dt.Rows[0][0]) == null) { }
              //  else
                {
                    byte[] imageData = (byte[])(dt.Rows[0][10]);
                    Image newImage;
                    //Read image data into a memory stream
                    using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                    {
                        ms.Write(imageData, 0, imageData.Length);

                        //Set image variable value using memory stream.
                        newImage = Image.FromStream(ms,true);
                        
                    }
                    pic0.Image = newImage;
                }
            }
        }



问题在行



ploblem is shown in line

newImage= Image.FromStream(ms,true);<br />


中显示 错误是参数无效
我无法调试问题

请帮助我


谢谢你
Aliza


the error is parameter not valid
i could not debug the problem

pleaase help me


thank u
Aliza

推荐答案

这是一个问题:您写入流,并立即尝试通过FromStream进行读取,但是流位置在最后.请执行以下操作:不要写入流;您已经有数据.使用不同的构造函数来创建不可调整大小的流MemoryStream(byte[]),并从中读取数据.请参阅 http://msdn.microsoft.com/en-us/library/system.io .memorystream.aspx [ ^ ].

—SA
Here is a problem: you write to stream and immediately try to read via FromStream, but the stream position is at the end. Do the following: do not write to the stream; you already have data. Use different constructor to create a non-resizeable stream, MemoryStream(byte[]) and just read from it. See http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

—SA


这篇关于将数据库中的图像数据读取到图片框中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆