加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将字节数组转换为图像时,参数无效

发布时间:2020-12-15 22:00:53 所属栏目:百科 来源:网络整理
导读:我想在访问数据库中保存图像,我使用OLE对象. 我们的想法是将图像转换为字节数组,然后在数据库中添加bytes数组. 这是功能: public static byte[] ImageToByte(Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.Conv
我想在访问数据库中保存图像,我使用OLE对象.

我们的想法是将图像转换为字节数组,然后在数据库中添加bytes数组.

这是功能:

public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img,typeof(byte[]));
    }

这很好用.

当我想将bytes数组返回给图像时,我得到一个异常:

参数异常未得到处理
参数无效.

我尝试了两个函数来将bytes数组转换为image:

public static Image ImageFromByte(byte[] image)
    {
        ImageConverter ic = new ImageConverter();
        Image img = (Image)ic.ConvertFrom(image);//here the exception comes
        return img;
    }

要么:

public static Image ImageFromByte1(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);//here the exception comes
        return returnImage;
    }

有什么问题,如何解决?

解决方法

尝试将图像保存到磁盘,看看是否有效.根据需要更改文件名和扩展名.

这些方面的东西:

string path = @"c:myimage.jpg";
            using (MemoryStream inputStream = new MemoryStream(byteArrayIn))
            {
                using (Stream file = File.Create(path))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int len;
                    while ((len = inputStream.Read(buffer,buffer.Length)) > 0)
                    {
                        file.Write(buffer,len);
                    }
                } 
            }

编辑:将图像写入磁盘工作,我想你可以看到图像.试试这个:

using (MemoryStream inputStream = new MemoryStream(byteArrayIn))
            {
                using (var image = Image.FromStream(inputStream))
                {
                    // see if this works.
                    // handle the image as you wish,return it,process it or something else.
                } 
            }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读