How Can I Convert an Array of Bytes into an Image in C#?

  • Thread starter Thread starter Jimmy Snyder
  • Start date Start date
Click For Summary
The discussion centers on displaying multiple 32x32 black-and-white images stored in byte arrays within a Windows Forms application using C#. Each image should be rendered in different colors, such as red, green, or blue, with the goal of displaying around 200 images simultaneously. The user seeks an efficient method to convert the byte arrays into images without drawing each pixel individually. Suggestions include using the System.Drawing.Image or System.Drawing.Bitmap classes, which allow for setting a color palette to display the images in the desired colors. The user is informed that GetDIBits and DeleteObject are not applicable in C#.NET, as they are older C functions. Instead, it is recommended to utilize a System.IO.MemoryStream to handle the pixel data and create the Bitmap or Image from that stream, streamlining the process of image rendering in the application.
Jimmy Snyder
Messages
1,122
Reaction score
22
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.
DeleteObject(hBitmap);

It's the Do something part that I need help with.
 
Technology news on Phys.org
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.

http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
 
Poor Jimmy. Bitmaps under Windows are a nightmare.
 
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?
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
9
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
6K