Reading monochrome color bitmaps

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);

    //Vertically
    for (int y = 0; y < bitmap.Height; y++)
    {
        //Horizontally
        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;
}
+3
7

a, .

y=0.3*R+0.59G*G+0.11*B

, 127:

const int threshold = 127;
Bitmap bm = { some source bitmap };

byte[,] buffer = new byte[64,256];

for(int y=0;y<bm.Height;y++)
{
  for(int x=0;x<bm.Width;x++)
  {
   Color c=source.GetPixel(x,y);
   int luminance = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
   buffer[x,y] = (luminance  > 127) ? 1 : 0;
  }
}
+6

#. . .

  • , . , , , ..

  • , , . > 0 1.

  • , . (64 * 256)/8. 1, 1, reset 0.

: 3. .

+1

GetPixel ! . LockBits, . , PixelFormat Format1bppIndexed, .

+1

pixelformat :

var bitmap = new Bitmap(256, 64, PixelFormat.Format1bppIndexed);

, :

graphics.SmoothingMode=SmoothingMode.None;

YMMV.

+1

GetPixel, . .

Windows (.. Graphics.FromImage) 24 bpp ( , 32? , ). , GetPixel Color, . :

MyBitmapFormat ToMyBitmap(Bitmap b)
{
    MyBitmapFormat mine = new MyBitmapFormat(b.Width, b.Height);

    for (int y=0; y < b.Height; y++) {
        for (int x=0; x < b.Width; x++) {
            mine.SetPixel(x, y, ColorIsBlackish(b.GetPixel(x, y)));
        }
    }
}

bool ColorIsBlackish(Color c)
{
    return Luminance(c) < 128; // 128 is midline
}

int Luminance(c)
{
    return (int)(0.299 * Color.Red + 0.587 * Color.Green + 0.114 * Color.Blue);
}

. braindead, .

+1

- 2d, 1- 0-, - 8x8 bmp am

myGrid =GetLedBytes(myBmp);
     for (int x = 1; x < 8; x++)
     {
         textBox1.Text = textBox1.Text + Convert.ToString(myGrid[x])+ "  ";
     }

:

225  231  231  231  231  129  255  

, 0 1?

+1

, - . SaveImage .

0

Source: https://habr.com/ru/post/1704064/


All Articles