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
像素格式。
那么如果我不能使用 Graphics 类,我该使用什么呢?
编辑2:阅读此,我认为可以安全地假设在使用 GDI+ 时我最好坚持使用 JPG 文件,不是吗?
编辑3:我的使用语句:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
Working in Visual Studio 2008. I'm trying to draw on a PNG image and save that image again.
I do the following:
private Image img = Image.FromFile("file.png");
private Graphics newGraphics;
And in the constructor:
newGraphics = Graphics.FromImage(img);
Building the solution gives no errors. When I try to run it, I get this:
A Graphics object cannot be created
from an image that has an indexed
pixel format.
I don't have much experience with using images in C#. What does this mean and how can I remedy this?
EDIT: through debugging, Visual Studio tells me that the image has a format8bppindexed
Pixel Format.
So if I can't use the Graphics class, what do I use?
EDIT2: After reading this, I think it's safe to assume that I better stick to JPG files when working with GDI+, no?
EDIT3: my using-statements:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法从索引图像格式(PNG、GIF...)创建图形。
您应该使用位图(将图像文件或转换为位图)。
You cannot create a graphics from an indexed image format (PNG, GIF,...).
You should use a Bitmap (file or convert your image to a bitmap).
如果没有更好的支持索引 PNG 的 PNG 库,您将无法尝试绘制该图像,因为显然 GDI+ 图形对象不支持索引图像。
如果您不需要使用索引 PNG,您可以捕获该错误并使用第 3 方实用程序将输入转换为常规 RGB PNG。
编辑:
我确实找到了这个链接 http://fci -h.blogspot.com/2008/02/c-indexed-pixel-problem.html 提供了一种在图像上绘图的方法,但是它不会影响原始图像,只是可以保存的副本( )如果您需要。
如果链接断开:
Without a better PNG library that supports indexed PNGs you're out of luck trying to draw to that image since evidently the GDI+ graphics object doesn't support indexed images.
If you don't need to use indexed PNGs you could trap that error and convert your input to regular RGB PNGs using a 3rd party utility.
edit:
I did find this link http://fci-h.blogspot.com/2008/02/c-indexed-pixel-problem.html that gives a method to draw on your image, however it won't affect the original, just a copy you can Save() if you require.
In case the link goes down: