Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;

And I just want to show the image from the byte capture that I have read before. And I use the code below, and I wonder why I always get the same error on the third line, and it is said : An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
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;
Updated 29-Sep-11 8:33am

There might be some problem with type casting

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());
Share this answer
Member 7955849 22-Aug-11 15:01pm    
I have tried your solution, but it throw the same error from before.. can you help me?
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?
Share this answer
Member 7955849 22-Aug-11 15:00pm    
Yeah, I also think that the main problem is the Image.FromStream, but unfortunately, I don't have any code that works before. This is my first project in image processing. So I just search the references from the internet and I haven't been found anything that can help me with my problem..
OriginalGriff 22-Aug-11 15:04pm    
frame = CamInput.QueryFrame();
pictureBox1.Image = frame.ToBitMap(pictureBox1.Width, pictureBox1.Height);
This works I assume - at least you don't say it doesn't. Which means it generates an image...
Member 7955849 22-Aug-11 15:08pm    
oh yeah. it is work. but it is only show the image which is captured from the webcam. meanwhile, what I want is that the image in the pictureBox1 is also displayed in pictureBox2, but I want it is displayed from the byte capture. because I want to build a project for video compressing in realtime process.
MSDN says

You must keep the stream open for the lifetime of the Bitmap.

Due to a limitation of the GDI+ decoder, an System.ArgumentException is thrown if you construct a bitmap from a .png image file with a single dimension greater than 65,535 pixels.

See it Here[^]
Share this answer
Member 7955849 22-Aug-11 15:24pm    
its not working mate.. huhu..
public static Image<bgr,> ConvertByteToImage(byte[] bytes)
return new Image<bgr,>(new Bitmap(Image.FromStream(new MemoryStream(bytes))));

public static byte[] ConvertImageToByte(Image<bgr,> My_Image)
MemoryStream m1 = new MemoryStream();
new Bitmap(My_Image.ToBitmap()).Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] header = new byte[] { 255, 216 };
header = m1.ToArray();
return (header);

hope useful to you..
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900