Manually created byte array to picture box.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Manually created byte array to picture box.

  1. #1
    Join Date
    Mar 2010
    Posts
    11

    Exclamation Manually created byte array to picture box.

    Hi everyone,

    I'm fairly new to C#. I have created a byte array which has elements which I have manually specified. Specifically, these bytes correspond to BGR components of an image (again, I emphasise that I have manually specified the elements.

    I would like to display my byte array as an image in a picture box. I've researched a lot, but have only found how to convert a byte array to a picture box image provided the byte array has been created by reading an image from file etc, but not manually created.

    I figure my issue has something to do with image header information being absent.

    Any help would be greatly appreciated.

    Faithfully,
    Antonio.

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

    Re: Manually created byte array to picture box.

    Yes, images follow a certain format, they are not simply made of pixel data. If you are going to manually create images you should either do it in code on a Bitmap object or read up on various image formats and create it correctly.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Mar 2010
    Posts
    11

    Re: Manually created byte array to picture box.

    Hi,

    Thanks for the clarification. So say I have the byte array that I've manually specified, and I create a new bitmap object, how would I go about correctly entering the pixel data into the bitmap object, (and header info if necessary), so that it can be displayed in a picture box.

    Sincerely,
    Antonio.

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

    Re: Manually created byte array to picture box.

    You don't, the library takes care of that for you. Create a bitmap object and start setting pixel values. When you save it you specify a format and .NET takes care of the rest. This is great because it saves us a ton of time, but it is unfortunate that many programmers have no clue as to how these things actually work behind the scenes.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Mar 2010
    Posts
    11

    Re: Manually created byte array to picture box.

    Thanks Ed, that worked. I guess the invisible behind-the-scenes work is what makes high level programming so attractive.

    Cheers.

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

    Re: Manually created byte array to picture box.

    BTW, if you ever find that Bitmap.Get/SetPixel is too slow (it is pretty slow, image analysis is tough with those) you can call LockBits on the bitmap and get a pointer to memory. Just remember that windows stores them in BGR order!
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  7. #7
    Join Date
    Apr 2011
    Posts
    20

    Resolved Re: Manually created byte array to picture box.

    You can display a byte array with manually defined RGB values with the following code:

    [code]
    int width = 320;
    int height = 240;
    byte[] imageData = new byte[width*height*3];

    // set RGB values manually
    .................
    .....................

    // display the image within the picture box

    //Here create the Bitmap to the know height, width and format
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);

    //Create a BitmapData and Lock all pixels to be written
    BitmapData bmpData = bmp.LockBits(
    new Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.ReadWrite, bmp.PixelFormat);

    //Copy the data from the byte array into BitmapData.Scan0
    Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);


    //Unlock the pixels
    bmp.UnlockBits(bmpData);

    this.m_PicView.Image = bmp;
    [\code]

Tags for this Thread

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