Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

From 1s and 0s to picturebox

  1. Jul 1, 2012 #1
    This is for a project I am working on using windows forms and c#. I have some data in an array of bytes. It is a b&w image of ones and zeros, 8 pixels to a byte. 32 pixels by 32 pixels. I have many such arrays and would like to display them in a picturebox in various colors. In otherwords, one of the 32x32 images would be red, another green, a third blue, etc, with perhaps 200 such images on the screen at once. One way would be to draw them pixel by pixel into the component, but this seems slow. Is there some way I could just send an array of bytes to some method that would magically turn the entire array into an image? How would you go about it? I don't need a detailed description, just a rough roadmap would suffice.

    GetDIBits looks like it might work, but when I try to use it, the visual C# compiler says "The name GetDIBits does not exist in the current context"

    Code examples for GetHBitmap look like this:

    IntPtr hBitmap = bm.GetHbitmap(Color.Blue);
    // Do something with hBitmap.

    It's the Do something part that I need help with.
  2. jcsd
  3. Jul 1, 2012 #2
    Maybe with System.Drawing::Image, which can be initialized with a System.Drawing::Bitmap. You can draw an Image onto a Form using DrawImage(Image, Point). Is the bitmap you are using that bitmap class? Tbh, I don't have much to do with C#, so I'm just guessing here.

  4. Jul 2, 2012 #3


    User Avatar

    Staff: Mentor

    Poor Jimmy. Bitmaps under Windows are a nightmare.
  5. Jul 2, 2012 #4


    User Avatar
    Homework Helper

    Not sure if this will help, but can't icons be 32 bit by 32 bit images and if so, is there a way icons could be used to display the images?
  6. Jul 2, 2012 #5

    I like Serena

    User Avatar
    Homework Helper

    Hi Jimmy.

    In C#.NET you should use System.Drawing.Image or System.Drawing.Bitmap.
    Each of those has a Palette that you can set.
    Set the Palette with the colors you want and displaying it should give you you B&W bitmap in the color you want.

    The function GetDIBits you mention is not a C#.NET function, but an old-style C function.
    The functioncall DeleteObject(hBitmap) is also an old-style C function.
    It is not needed when programming in C#.NET.

    To get the pixels in, you should create a System.IO.MemoryStream with your pixel data, and create the Bitmap or Image from that.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook