本文介绍了水印.gif和.tif图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨.. 我正在使用带有.png图像的C#对图像进行水印。 所有.jpg ,. bmp,.png图像完美地加水印但是在加水印时.tif 和.gif图像错误发生。 错误信息:A无法从具有索引像素格式的图像创建图形对象。 $。b $ b除了.tif和.gif以外的图像 FileStream fs = new FileStream(img,FileMode.Open,FileAccess.Read); image = Image.FromStream(fs); Graphics imageGraphics = Graphics.FromImage(image); watermarkBrush.TranslateTransform(x,y); imageGraphics.FillRectangle(watermarkBrush, new 矩形( new Point(x,y), new 大小(finalWaterImage.Width + 1 ,finalWaterImage.Height))); var filename = Path.GetFileName(img); image.Save(outputFolder + \\ + filename); for .gif我做了这个代码并且它有效: < pre lang = c#> FileStream fs = new FileStream(CurrentFile,FileMode.Open,FileAccess.Read); image = Image.FromStream(fs); 位图位= 新位图(image.Width,image.Height); Graphics newGraphics = Graphics.FromImage((Image)bit); newGraphics.DrawImage(image, 0 , 0 ,image.Width, image.Height); image = bit; Graphics imageGraphics = Graphics.FromImage(image); watermarkBrush.TranslateTransform(x,y); imageGraphics.FillRectangle(watermarkBrush, new 矩形( new Point(x,y), new 大小(finalWaterImage.Width + 1 ,finalWaterImage.Height))); 但这不适用于.tif .gif图片出现上述错误。 现在.if图像正常工作,在传递.tif图像后,我收到一条错误,说参数在此行无效: image = Image.FromStream(fs); 解决方案 请参阅我对该问题的评论。你不能这样问问题。 但是,我想我知道如何解决这个问题。我的猜测是:你的异常是在这个电话中抛出的: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx [ ^ ]。 如果有效对于大多数图像,但您碰巧遇到索引格式的图像,这是不受支持的(这很自然)。在你的一个用户做过之前,你很幸运能够抓住这个案例。你不应该使用它。 如何解决这个问题?在进行此调用之前,您只需将位图转换为 Graphics 支持的某种统一像素格式。最简单的方法是 System.Drawing.Bitmap.Clone : http://msdn.microsoft.com/en-us/library/ms141944.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/ezs7679t.aspx [ ^ ]。 这个应该解决你的问题。如果没有,请不要怪我。您应该已经提供了代码示例和重现问题的方法。 -SA 由于GDI +图形对象不支持索引图像。我假设您在代码中使用 Image ,以便最终获得该异常。要克服它,你应该将图像转换为位图。 尝试 { Image img = Image.FromStream( new MemoryStream (图片)); // 或者从文件我不知道如何加载它 Bitmap img = new 位图( new 位图(img)); // 进行图像处理 } catch (例外情况) { // 做东西 } 祝你好运, OI Hi..I''m watermarking images using C# with a .png image.all the .jpg, .bmp, .png images are getting watermarked perfectly but when watermarking .tifand .gif images error occurs.error message : "A Graphics object cannot be created from an image that has an indexed pixel format."for images other than .tif and .gifFileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read); image = Image.FromStream(fs);Graphics imageGraphics = Graphics.FromImage(image); watermarkBrush.TranslateTransform(x, y); imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(finalWaterImage.Width + 1,finalWaterImage.Height))); var filename = Path.GetFileName(img); image.Save(outputFolder + "\\" + filename);for .gif i did this code and it worked :<pre lang="c#">FileStream fs = new FileStream(CurrentFile, FileMode.Open, FileAccess.Read); image = Image.FromStream(fs); Bitmap bit = new Bitmap(image.Width, image.Height); Graphics newGraphics = Graphics.FromImage((Image)bit); newGraphics.DrawImage(image, 0, 0, image.Width, image.Height); image = bit; Graphics imageGraphics = Graphics.FromImage(image); watermarkBrush.TranslateTransform(x, y); imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(finalWaterImage.Width + 1,finalWaterImage.Height)));but this aint working for .tifthe above error occured for .gif images.now that .if images are working, after passing .tif image i get an error saying "parameter not valid at this line : image = Image.FromStream(fs); " 解决方案 Please see my comment to the question. You cannot ask questions this way.However, I think I know how to solve the problem. My guess is: your exception is thrown in this call:http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].If works for most images, but you happen to come across the image with indexed format, which is not supported (which is quite natural). You were very lucky to catch this case before one of your users did. You should not use it.How to fix the situation? Before doing this call, you simply need to convert the bitmap to some unified pixel format which is supported by Graphics. The simplest way to do it would be System.Drawing.Bitmap.Clone:http://msdn.microsoft.com/en-us/library/ms141944.aspx[^],http://msdn.microsoft.com/en-us/library/ezs7679t.aspx[^].This should solve your problem. If not, please don''t blame me. You should have provided a code sample and a way to reproduce the problem.—SASince GDI+ graphics object doesn''t support indexed images. I assume you were using Image in your code so that you end up getting that exception. To overcome it you should convert your Image to Bitmap.try{ Image img = Image.FromStream(new MemoryStream(image)); // OR from file I dont know how you load it Bitmap img = new Bitmap(new Bitmap( img )); //do your image processing}catch (Exception ex){ // do stuff}Good luck,OI 这篇关于水印.gif和.tif图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 05:43