2012-09-01 60 views
0

大家我想將C++代碼轉換爲c#。 這個壓縮位圖圖像的代碼。我的C++代碼使用BITMAPINFOHEADER
來讀取位圖圖像的biBitCount,我如何獲得c#中圖像的位數(win app)? 這是C++代碼C++ BITMAPINFOHEADER biBitCount在C#中的等價物

char *pTemp; //image data will store here 
BITMAPINFOHEADER *pbminfo; 
pbminfo = (BITMAPINFOHEADER *)pTemp; 
if (pbminfo->biBitCount != 1) // Convert 24 bit -> 1 bit 
{ 
    //some process will be done here 
} 
+0

重寫將是重大的。不要丟棄可用的代碼,使用C++/CLI語言爲您的C++代碼編寫託管類封裝器。 –

回答

0

你確實可以寫在Visual C++/CLI的包裝類,但僅使用C#在你的最終你會想要做的一切是完全可行的。

您可以使用圖像的PixelFormat屬性。它有各種各樣的possible values

像素格式定義與一個數據像素關聯的內存位數。該格式還定義了單個像素數據中顏色分量的順序。

PixelFormat48bppRGB,PixelFormat64bppARGB和PixelFormat64bppPARGB每個顏色分量(通道)使用16位。 GDI +版本1.0和1.1可以讀取每通道16位的圖像,但這些圖像會轉換爲每通道8位的格式進行處理,顯示和保存。每個16位顏色通道可以保存0到2^13範圍內的值。

某些像素格式包含預乘顏色值。預乘表示顏色值已經乘以阿爾法值。

var image = new Bitmap(@"C:\temp\me.png"); 
if (image != null) { 
    Console.Write("Format: {0}", image.PixelFormat.ToString("G")); 
} 

在我的情況下,其結果將是:Format32bppArgb

0
using System.Drawing; 

pubic class TestThis 
{ 
    public void Test() 
    { 
     Image myImage = Image.FromFile("Myfile.png"); 

     int bitDepth = Image.GetPixelFormatSize(myImage.PixelFormat); 

     if(bitDepth != 1) 
     { 
      // Do domething 
     } 
    } 
}