c++ BITMAPINFOHEADER biBitCount equivalent in C#

c++ BITMAPINFOHEADER biBitCount equivalent in C#

本文关键字:in equivalent biBitCount BITMAPINFOHEADER c++      更新时间:2023-10-16

我正在尝试将c++代码转换为c#的每个人。该代码正在压缩位图图像。我的c++代码使用BITMAPINFOHEADER
要读取位图图像的biBitCount,我如何在c#(win-app)中获得图像的位数?。这是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
}

您确实可以在Visual C++/CLI中编写一个包装器类,但您想做的一切都可以完全实现,只需在您的端使用C#。

可以使用图像的PixelFormat属性。它有多种可能的价值。

像素格式定义与数据的一个像素相关联的存储器的位数。该格式还定义了数据的单个像素内的颜色分量的顺序。

PixelFormat48bppRGB、PixelFormat64bppARGB和PixelFormat64 bppPARGB每个颜色分量(通道)使用16位。GDI+版本1.0和1.1可以读取每个通道16位的图像,但这些图像被转换为每个通道8位的格式,用于处理、显示和保存。每个16位颜色通道都可以保存0到2^13范围内的值。

某些像素格式包含预乘的颜色值。预乘表示颜色值已经乘以alpha值。

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

在我的情况下,结果将是:Format32bppArgb

您可以尝试以下操作:

using System.Drawing;
public class TestThis
{
    public void Test()
    {
        Image myImage = Image.FromFile("Myfile.png");
        int bitDepth = Image.GetPixelFormatSize(myImage.PixelFormat);
        if( bitDepth != 1)
        {
            // Do domething
        }
    }
}