2011-03-21 122 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