2011-03-21 113 views
0

原始像素值我怎样才能得到每像素位数的位图图像?例如,如果像素x = 24 & Y = 45的RGB 123212112所以它必须返回1111011,11010100,1110000获取位图图像

+0

这不是每个像素的位数;每像素位数是图像数据可以编码的颜色数量的函数(或者如果您愿意,也可以采用其他方式)。改进标题。 – 2011-03-21 12:36:58

回答

0

你的问题是不特定的像素,基本上需要得到比特字节:

您可以使用一个静态扩展:

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

将文件加载到一个Bitmap,得到Pixel,并从中读出Color信息,这将让你每个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关于如何将其转换为二进制字符串的答案。我认为这个问题并不完全清楚你需要什么。

+3

'GetPixel'是*非常*慢,所以如果你需要循环位图中的所有像素不使用此。 – CodesInChaos 2011-03-21 12:52:39

1

为了得到位每像素使用此功能:

Image.GetPixelFormatSize(bitmap.PixelFormat) 

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