问题在C#PNG图像 [英] Problem with PNG images in C#

查看:308
本文介绍了问题在C#PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2008中我试图再次提请一个PNG图像并保存图像工作



我做到以下几点:

 私人图片IMG = Image.FromFile(file.png); 
私有图形newGraphics;

和在构造函数中:

  newGraphics = Graphics.FromImage(IMG); 



构建解决方案没有给出错误。当我尝试运行它,我得到这样的:




一个图形对象不能从具有索引的形象创造了

像素格式。




我没有在C#中使用的图像太多经验。这是什么意思,我怎么能解决这个问题。



编辑:?通过调试,Visual Studio中告诉我,像有一个 format8bppindexed 像素格式。



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



EDIT2:在阅读的这个,我认为它是安全的假设,我更好地坚持JPG文件与GDI时+,没有



EDIT3:我使用语句:

 使用系统; 
使用System.Collections.Generic;
使用System.Drawing中;
使用System.Drawing.Imaging;使用System.Windows.Forms的
;


解决方案

不支持索引PNG图像你更好的PNG库因为看样子GDI重新倒霉试图绘制到该图像+图形对象不支持索引图像。



如果您不需要使用索引的PNG图片你可以。陷阱,错误和你的输入转换为使用第三方工具定期RGB PNG格式



编辑:



我没有找到这个链接 HTTP://fci-h.blogspot .COM / 2008/02 / C-索引象素problem.html ,让绘制图像上的方法,但它不会影响原有的,只是一个副本,你可以保存()如果您需要



在的情况下链接中断:

 位图BM = (位图)System.Drawing.Image.FromFile(FCI-h.jpg,真正的); 
位图TMP =新位图(bm.Width,bm.Height);
图形grPhoto = Graphics.FromImage(TMP);
grPhoto.DrawImage(宽多重峰,新的Rectangle(0,0,tmp.Width,tmp.Height),0,0,tmp.Width,tmp.Height,GraphicsUnit.Pixel);


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;

解决方案

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:

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);

这篇关于问题在C#PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆