简体   繁体   English

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

[英]Number of channels in System.Drawing.Bitmap C#

I'm coding in C#, and loading images this way : 我正在用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);

I need to know the number of channels in my image (usually they are grayscale (1), RGB (3) or RGBA (4)), and I don't know how is that information storred. 我需要知道图像中的通道数(通常是灰度(1),RGB(3)或RGBA(4)),而且我不知道该信息如何存储。

EDIT : I'm not looking to force a pixel format. 编辑:我不是要强制像素格式。 I'm trying to load an image, and figure out procedurally what is the number of channel in the image I loaded. 我正在尝试加载图像,然后从程序中确定我加载的图像中的通道数是多少。

There is a PixelFormat property, you can read about in on MSDN . 有一个PixelFormat属性,您可以在MSDN上阅读。

You can combine with: GetPixelFormatSize it to get the bytes per pixel if you want. 您可以结合使用: GetPixelFormatSize如果需要,可以获取每个像素的字节数。

Creation: 创建:

You need to use an overload of the Bitmap contructor that takes a PixelFormat parameter. 您需要使用带PixelFormat参数的位图构造函数的重载

Usage: 用法:

The the bitmap.PixelFormat property will tell you what you have. bitmap.PixelFormat属性将告诉您所拥有的。

Here is more info about the PixelFormats 这是有关PixelFormats的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM