C#中的PNG图像问题

在Visual Studio 2008中工作。我正在尝试绘制PNG图像并再次保存该图像。

我做了以下事情:

private Image img = Image.FromFile("file.png"); private Graphics newGraphics; 

在构造函数中:

 newGraphics = Graphics.FromImage(img); 

构建解决方案不会产生错误。 当我尝试运行它时,我得到了这个:

无法从具有索引像素格式的图像创建Graphics对象。

我在C#中使用图像的经验不多。 这是什么意思,我该如何解决这个问题?

编辑:通过调试,Visual Studio告诉我该图像具有format8bppindexed Pixel Format。

所以如果我不能使用Graphics类,我该怎么用?

EDIT2:读完之后,我认为可以安全地假设我在使用GDI +时更好地坚持使用JPG文件,不是吗?

EDIT3:我的使用陈述:

 using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; 

如果没有更好的支持索引PNG的PNG库,那么你试图绘制到该图像是不合时宜的,因为显然GDI +图形对象不支持索引图像。

如果您不需要使用索引的PNG,则可以捕获该错误并使用第三方实用程序将输入转换为常规RGB PNG。

编辑:

我确实找到了这个链接http://fci-h.blogspot.com/2008/02/c-indexed-pixel-problem.html ,它提供了一种绘制图像的方法,但它不会影响原始图像,只是如果需要,可以保存()的副本。

如果链接断开:

 Bitmap bm = (Bitmap) System.Drawing.Image.FromFile("Fci-h.jpg",true); Bitmap tmp=new Bitmap (bm.Width ,bm.Height ); Graphics grPhoto = Graphics.FromImage(tmp); grPhoto.DrawImage(bm, new Rectangle(0, 0, tmp.Width , tmp.Height ), 0, 0, tmp.Width , tmp.Height , GraphicsUnit.Pixel); 

您无法从索引图像格式(PNG,GIF,…)创建图形。 您应该使用位图(文件或将图像转换为位图)。

 Image img = Image.FromFile("file.png"); img = new Bitmap(img); newGraphics = Graphics.FromImage(img);