reading image data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: reading image data

  1. #1
    Join Date
    Jun 2010
    Posts
    115

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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Jun 2010
    Posts
    115

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,238

    Re: reading image data

    Quote Originally Posted by Maejie View Post
    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.

  5. #5
    Join Date
    Jun 2010
    Posts
    115

    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(msImageFormat.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;


  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,238

    Re: reading image data

    Quote Originally Posted by Maejie View Post
    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?

  7. #7
    Join Date
    Jun 2010
    Posts
    115

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center