C.System.Drawing.Bitmap中的通道数

时间:2018-08-17 10:33:08

标签: c# image channels

我正在用C#编码,并以这种方式加载图像:

// loading image
string imageFileName = "myImage.jpg";
Bitmap bmp = new Bitmap(imageFileName, false);

// trying to display color so that I know how many channels I have
// except it always displays 4 values, whether I have 1, 3 or 4 channels
Color color = bmp.GetPixel(0, 0);
Console.WriteLine(color.ToString());

// locking the bitmap's bits
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

// doing stuff that requires me to know the number of channels of the image, amongst other things

// unlocking the bitmap's bits
bmp.UnlockBits(bmpData);

我需要知道图像中的通道数(通常是灰度(1),RGB(3)或RGBA(4)),而且我不知道该信息如何存储。

编辑: 我不是要强制使用像素格式。我正在尝试加载图像,然后从程序中找出我加载的图像中的通道数。

2 个答案:

答案 0 :(得分:1)

有一个PixelFormat属性,您可以在MSDN上阅读有关内容。

如果需要,可以与GetPixelFormatSize结合使用以获取每个像素的字节数。

答案 1 :(得分:0)

创作:

您需要使用带有PixelFormat参数的overload of the Bitmap contructor

用法:

bitmap.PixelFormat属性将告诉您您拥有什么。

Here is more info about the PixelFormats

相关问题