您的位置:首页 > 移动开发 > Objective-C

关于无法从带INDEX格式的GIF图片创建Graphics的解决方案

2006-07-03 21:25 429 查看
有位网友询问这样一个问题:
Stream upImageStream = ImageUpload.InputStream;//上传文件流
System.Drawing.Image orgImg = System.Drawing.Image.FromStream(upImageStream);//通过文件流获取原始图片
Bitmap zoomImg = new Bitmap(orgImg.Width, orgImg.Height, orgImg.PixelFormat);//创建图片(位图)//Format32bppArgb
Graphics myImg = Graphics.FromImage(zoomImg);//创建Graphics
在最后一句出现问题,老提示:A Graphics object cannot be created from an image that has an indexed pixel format.请问如何解决,谢谢!!

--------------------------------------------
产生这种错误的原因参见:
http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx中的相关说明。

Remarks
If the image has an indexed pixel format, this method throws an exception with the message, "A Graphics object cannot be created from an image that has an indexed pixel format." The indexed pixel formats are shown in the following list.

Format1bppIndexed

Format4bppIndexed

Format8bppIndexed

This method also throws an exception if the image has any of the following pixel formats.

Undefined

DontCare

Format16bppArgb1555

Format16bppGrayScale

解决方法:
你可以试试这样做:

Bitmap bm = new (Bitmap)Image.FromFile("YourFileName.gif");
Bitmap tmp = new Bitmap(bm.Width,bm.Height);
Graphics g = Graphics.FromImage(tmp);
g.DrawImage(bm, new Rectangle(0,0,tmp.Width,tmp.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixe
l);
g.DrawEllipse(Pens.Red,10,10,100,120); // 进行你的绘图操作

如果你想保存为新的图片格式,可以使用tmp.Save("你的文件名",你的格式参数)。
如果你想保存为原来的索引格式,你必须使用LockBits命令锁定Bitmap图片,然后进行你的图片绘制操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐