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

Discussion Overview

The discussion revolves around converting an array of bytes representing a black and white image into a displayable image format in C# for use in a Windows Forms application. Participants explore various methods for achieving this, including the use of System.Drawing classes and potential alternatives for displaying multiple images efficiently.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes the need to convert byte arrays of 32x32 pixel images into displayable images, expressing concern about performance when drawing pixel by pixel.
  • Another participant suggests using System.Drawing::Image initialized with System.Drawing::Bitmap, mentioning the ability to draw images onto a form.
  • A participant comments on the complexity of handling bitmaps in Windows, indicating that it can be challenging.
  • One participant proposes the idea of using icons as a potential method for displaying the images, questioning if they can be used for 32x32 images.
  • A later reply recommends using System.Drawing.Image or System.Drawing.Bitmap in C#.NET, noting that these classes have palettes that can be set for color display.
  • The same participant clarifies that GetDIBits and DeleteObject are not C#.NET functions and suggests using a MemoryStream to create a Bitmap or Image from the pixel data.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to convert byte arrays to images, with no consensus on a single method. Some suggest using System.Drawing classes, while others explore alternative ideas like icons.

Contextual Notes

There is uncertainty regarding the best practices for handling image data in C#, particularly with respect to performance and the use of legacy functions. The discussion includes various assumptions about the capabilities of different classes and methods.

Who May Find This Useful

Developers working with C# and Windows Forms who need to display images from byte arrays, as well as those interested in image processing techniques in .NET.

Jimmy Snyder
Messages
1,137
Reaction score
21
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.
 

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 6 ·
Replies
6
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
6K