2012-09-01 63 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 
     } 
    } 
}