简体   繁体   中英

Get raw pixel value in bitmap image

How can I get bits per pixel in bitmap image? example if pixel x=24 & y=45 has RGB 123 , 212 , 112 so it must return 1111011 , 11010100 , 1110000 .

Load the file into a Bitmap , get the Pixel , and read the Color information from it, which will get you a Byte for each of R, G and B.

Bitmap bmp = new Bitmap ("C:\image.bmp");
Color color = bmp.GetPixel(24, 45);
Debug.WriteLine (string.Format ("R={0}, G={1}, B={2}", color.R, color.G, color.B));

See Aliostad's answer for how to convert this into a binary string. I think the question isn't entirely clear on what you require.

To get the bits per pixel use this function:

Image.GetPixelFormatSize(bitmap.PixelFormat) 

For more information you can read this answer as well as this .

Your problem is not specific to pixels, basically need to get bits for the bytes:

You can use a static extension:

    public static string ToBitString(this byte b)
    {
        StringBuilder sb = new StringBuilder(8);
        for (int i = 7; i >= 0; i--)
        {
            sb.Append((b & (1 << i)) > 0 ? '1' : '0');
        }
        return sb.ToString();
    }

And use:

        byte bt = 120;
        Console.WriteLine(bt.ToBitString());
                    // outputs 01111000

In your case:

   Color c = ...;
   string s = ((byte) c.B).ToBitString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM