C#中的PNG图像问题

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

我做了以下事情:

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

在构造函数中:

newGraphics = Graphics.FromImage(img);

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

A Graphics object cannot be created
from an image that has an indexed
pixel format.

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

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

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

编辑2:阅读this之后,我认为可以安全地假设我在使用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,tmp.Width,tmp.Height ),tmp.Height,GraphicsUnit.Pixel);

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...