从字节数组中显示图像 [英] Show Image from byte array

查看:75
本文介绍了从字节数组中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..我想急切地问一下......

我们说我有一个来自图像的字节数组。我使用EmguCV作为我的库从我的网络摄像头捕获图片。我已经编写了这样的代码来捕获图片框中的图像:



Hi guys.. I want to ask something urgently..
Let's says that I have a byte array from image. I am using EmguCV as my library to capture picture from my webcam. And I have wrote the code like this to capture the image in a picturebox :

public Capture CamInput = new Capture();
public Image<bgr,> frame;
public Image<bgr,> render;
public byte[] ImageCapture;

frame = CamInput.QueryFrame();
pictureBox1.Image = frame.ToBitMap(pictureBox1.Width, pictureBox1.Height);

ImageCapture = frame.Bytes;





我只想显示我之前读过的字节捕获图像。我使用下面的代码,我想知道为什么我总是在第三行得到相同的错误,并且说: System.Drawing.dll中出现未处理的类型'System.ArgumentException'的异常

附加信息:参数无效。

MemoryStream ms = new MemoryStream(ImageCapture, 0, ImageCapture.Length);
    ms.Write(ImageCapture, 0, ImageCapture.Length);
    Bitmap b = (Bitmap)Image.FromStream(ms, true);
    render = new Image<bgr,>(b);
    pictureBox2.Image = render;

推荐答案

类型转换可能有问题



There might be some problem with type casting

Image.FromStream

返回类型为Image。尝试使用它。





位图始终是图像,但不是每个图像都是位图



或你可以这样使用它



return type is Image. try using that.


Bitmap is always an Image, but not every Image is a Bitmap

or u may use it in this way

For Convert you may use it:

 

static public Bitmap BitmapFromBitmapData(byte[] BitmapData)
{
MemoryStream ms = new MemoryStream(BitmapData);
return (new Bitmap(ms));
}

 
static public byte[] BitmapDataFromBitmap(Bitmap objBitmap, ImageFormat imageFormat)

{
MemoryStream ms = new MemoryStream();
objBitmap.Save(ms, imageFormat);
return (ms.GetBuffer());
}


之前我没有使用过EmguCV,但是如果你必须先将其转换为位图才能显示它,这意味着格式不是通常支持的格式之一。您是否考虑过使用ToBitMap方法而不是通过流?我怀疑是Image.FromStream抛出异常 - 如果你的代码有效,为什么不再使用呢?
I've not used EmguCV before, but if you have to convert it to a bitmap before you can display it, that implies that the format is not one of the normally supported ones. Have you considered using your ToBitMap method instead of going via the stream? I would suspect that it is the Image.FromStream that is throwing the exception - if you have code that works, why not use it again?


MSDN说



你必须在Bitmap的生命周期内保持流打开。



由于GDI +解码器的限制,System.ArgumentException如果你从一个大于65,535像素的单个维度的.png图像文件构造一个位图,则抛出。





在此查看 [ ^ ]


这篇关于从字节数组中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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