在.Net 2.0中读取PNG图像文件

时间:2020-03-06 14:25:02  来源:igfitidea点击:

我正在使用Cin .Net 2.0,我想读取一个PNG图像文件,并检查具有非透明像素的第一行和第一列。

我应该使用什么程序集和/或者类?

解决方案

当然我已经在Google上搜索并找到了PngBitmapDecoder类,但是它在.Net 2.0中似乎不可用?

http://msdn.microsoft.com/zh-CN/library/system.windows.media.imaging.pngbitmapdecoder.aspx

上面的链接提到它在PresentationCore程序集中,我似乎没有包含在.Net 2.0中

System.Drawing.dll程序集中的位图类:

Bitmap bitmap = new Bitmap(@"C:\image.png");
Color clr = bitmap.GetPixel(0, 0);

好吧,Bitmap类可以读取PNG文件并访问像素。可以看到透明像素吗? PNG支持透明,而BMP不支持。但是,它仍然有效。

Bitmap bitmap = new Bitmap("icn_loading_animated3a.png");
pictureBox1.Image = bitmap;
Color pixel5by10 = bitmap.GetPixel(5, 10);

上面的代码读取了我的小图片,然后读取了透明像素。颜色类别具有RGBA值,并且我读取的像素被识别为透明的。