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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/100247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:57:51  来源:igfitidea点击:

Reading a PNG image file in .Net 2.0

提问by Roy Tang

I'm using C# in .Net 2.0, and I want to read in a PNG image file and check for the first row and first column that has non-transparent pixels.

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

What assembly and/or class should I use?

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

采纳答案by Pavel Chuchuva

Bitmapclass from System.Drawing.dll assembly:

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

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

回答by Roy Tang

Of course I googled already and found the PngBitmapDecoder class, but it doesn't seem to be available in .Net 2.0?

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

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.pngbitmapdecoder.aspx

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.pngbitmapdecoder.aspx

The above link mentions it's in the PresentationCore assembly which I don't seem to have included with .Net 2.0

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

回答by ArekBulski

Well, Bitmap class can read a PNG file and access pixels. Can it see transparent pixels? PNG supports transparency while BMP does not. But still, it works.

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

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

Code above read my little picture and then read a transparent pixel. Color class has RGBA values, and the pixel I read was in recognized as transparent.

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