简体   繁体   English

获取位图图像中的原始像素值

[英]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 . 例如,如果像素x=24y=45具有RGB 123212112 ,以便它必须返回1111011110101001110000

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中 ,获取像素 ,并从中读取颜色信息,这将为R,G和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. 请参阅Aliostad的答案,了解如何将其转换为二进制字符串。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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