Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 C# EMGU 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;
 
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;
Posted 22-Aug-11 9:11am
Edited 29-Sep-11 9:33am
C_Johnson38.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There might be some problem with type casting
 
Image.FromStream
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());
}
 
  Permalink  
Comments
Member 7955849 at 22-Aug-11 15:01pm
   
I have tried your solution, but it throw the same error from before.. can you help me?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Comments
Member 7955849 at 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 at 22-Aug-11 15:04pm
   
Um...
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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Comments
Member 7955849 at 22-Aug-11 15:24pm
   
its not working mate.. huhu..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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..
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 587
2 Maciej Los 325
3 Mathew Soji 195
4 BillWoodruff 190
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 8 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100