Get raw pixel value in bitmap image
How can I get bits p开发者_如何转开发er pixel in bitmap image? example if pixel x=24
& y=45
has RGB 123
,212
,112
so it must return 1111011
, 11010100
, 1110000
.
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.
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.
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();
精彩评论