繁体   English   中英

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

[英]Get raw pixel value in bitmap image

如何获取位图图像中的每像素位数? 例如,如果像素x=24y=45具有RGB 123212112 ,以便它必须返回1111011110101001110000

将文件加载到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));

请参阅Aliostad的答案,了解如何将其转换为二进制字符串。 我认为问题并不完全清楚你需要什么。

要获得每像素位数,请使用此函数:

Image.GetPixelFormatSize(bitmap.PixelFormat) 

欲了解更多信息,你可以阅读这个答案,以及这个

您的问题并非特定于像素,基本上需要获取字节的位:

您可以使用静态扩展:

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

并使用:

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

在你的情况下:

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

暂无
暂无

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

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