繁体   English   中英

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

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

我正在用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)),而且我不知道该信息如何存储。

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

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

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

创建:

您需要使用带PixelFormat参数的位图构造函数的重载

用法:

bitmap.PixelFormat属性将告诉您所拥有的。

这是有关PixelFormats的更多信息

暂无
暂无

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

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