I do not know a better name, but I will talk about the problem.
Part of the equipment used has the ability to display images. It can display a black and white image with a resolution of 64 x 256.
The problem is the image format we need to send to the device. This is not a standard raster image format, but instead is just an array of bytes representing each pixel in the image.
0 = black, 1 = white.
So, if we had an image with a size of: 4 x 4, an array of bytes could look something like this:
1000 0100 0010 0001
And the image will look like this:
Bitmap image http://www.mediafire.com/imgbnc.php/6ee6a28148d0170708cb10ec7ce6512e4g.jpg
The problem is that we need to create this image by creating a monochrome bitmap in C # and then convert it to a file format that the device understands.
For example, you can display text on a device. To do this, he needs to create a bitmap image and write him the text:
var bitmap = new Bitmap(256, 64);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.DrawString("Hello World", new Font("Courier", 10, FontStyle.Regular), new SolidBrush(Color.White), 1, 1);
}
There are two problems here:
- Generated bitmap is not monochrome
- The generated bitmap has a different binary format
I need a way:
- Creating a monochrome bitmap in .NET
- Read the individual pixel colors for each pixel in the bitmap
I found that you can set the pixel depth to 16, 24 or 32 bits, but have not found a monochrome image, and I have no idea how to read the pixel data.
Suggestions are welcome.
: Win32 PInvokes... !
FOLLOW UP: . ( - )
private static byte[] GetLedBytes(Bitmap bitmap)
{
int threshold = 127;
int index = 0;
int dimensions = bitmap.Height * bitmap.Width;
BitArray bits = new BitArray(dimensions);
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color c = bitmap.GetPixel(x, y);
int luminance = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bits[index] = (luminance > threshold);
index++;
}
}
byte[] data = new byte[dimensions / 8];
bits.CopyTo(data, 0);
return data;
}