February 12th, 2011, 11:57 PM
#1
reading image data
I read an image into a byte array this way
PHP Code:
for i = 0 to WIDTH
for j = 0 to HEIGHT
data [ i , j ]=( byte ) img . GetPixel ( i , j ). ToArgb ();
but when I show it, the image becomes gray . I am so confused.
Could someone help me please ? Thank you.
February 13th, 2011, 06:25 AM
#2
Re: reading image data
Wrell, ToArgb doesn't return a byte, it returns an int, so you are overflowing. If you want a byte array you can do this:
Code:
MemoryStream ms = new MemoryStream();
bmp.Save (ms, someFormat);
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
Also, if your images are even moderately sized, looping through each pixel using GetPixel and SetPixel is going to be really slow.
February 13th, 2011, 10:19 AM
#3
Re: reading image data
Thank you BigEd781,
I do as what you suggest me,
I now don't know how to change the byte array back into an image ?
I have tried
Image img=Image.FromStream(ms);
but the compiler states that ms is an invalid argument in the debug view, although I find that all of its properties are initialised.
February 13th, 2011, 01:11 PM
#4
Re: reading image data
Originally Posted by
Maejie
Thank you BigEd781,
I do as what you suggest me,
I now don't know how to change the byte array back into an image ?
I have tried
Image img=Image.FromStream(ms);
but the compiler states that ms is an invalid argument in the debug view, although I find that all of its properties are initialised.
Did you call ms.Close(); as Big Ed suggested?
Once you call close on the memory stream you can't use it anymore.
February 14th, 2011, 01:16 AM
#5
Re: reading image data
Thank you for your reply,
I do it like this
PHP Code:
byte [] UseStreamToByte ( Image img )
{
MemoryStream ms =new MemoryStream ();
img . Save ( ms , ImageFormat . Png );
byte [] temp = ms . GetBuffer ();
ms . Closer ();
return temp ;
}
///do some processing
Image UseStreamToImg ( byte [] imgDt )
{
MemoryStream ms =new Memorystream ( imgDt );
Image img = ms . FromStream ( ms ); // this is where I get the error "invalid parameter/argument ms"
ms . Close ();
return img ;
}
February 14th, 2011, 10:36 AM
#6
Re: reading image data
Originally Posted by
Maejie
Code:
Image UseStreamToImg(byte[] imgDt)
{
MemoryStream ms=new Memorystream(imgDt);
Image img=ms. FromStream(ms); // this is where I get the error "invalid parameter/argument ms"
ms.Close();
return img;
}
Should the red highlighted portion be Image instead of ms?
February 16th, 2011, 10:37 AM
#7
Re: reading image data
Thank you, yes it was my typo, it should have been |Image.FromStream(ms)| but still I have that exception irresolvable.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks