【问题标题】:C# watermarking not working for .gif and .tif imagesC# 水印不适用于 .gif 和 .tif 图像
【发布时间】:2013-05-03 15:49:32
【问题描述】:

我正在尝试使用 .png 图像为图像添加水印 我的 .jpg 和 .png 图像正确添加了水印,但 .tif 和 .gif 图像出现错误。

我在 C# 中做。 我得到了这个错误:

{"不能从具有 索引像素格式。在 System.Drawing.Graphics.FromImage(图像 图片)在 Watermarking.test_image_watermak.button1_Click(Object 发件人,EventArgs e) "}

我的代码

FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
                    image = Image.FromStream(fs);

                    int newWaterWidth = (int)((float)image.Width * .30);


                    int newWaterHeight = (newWaterWidth * watermarkImage.Height) / watermarkImage.Width;

                    Bitmap resizedWaterm = new Bitmap(newWaterWidth, newWaterHeight);
                    Graphics g = Graphics.FromImage((Image)resizedWaterm);

                    g.DrawImage(watermarkImage, 0, 0, newWaterWidth, newWaterHeight);

                    ***Graphics imageGraphics = Graphics.FromImage(image);*** //error at this line

                    TextureBrush watermarkBrush = new TextureBrush((Image)resizedWaterm);
                    int x = (image.Width / 2 - resizedWaterm.Width / 2);

                    int y = (image.Height / 2 - resizedWaterm.Height / 2);

                    watermarkBrush.TranslateTransform(x, y);

                    imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(resizedWaterm.Width + 1, resizedWaterm.Height)));
                    var filename = Path.GetFileName(img);

                    image.Save(outputFolder + "\\" + filename);

对于这些类型的图像,您需要做些什么不同的事情???

【问题讨论】:

  • 我会尝试,但我认为这行不通....谢谢@codecaster
  • 无论如何你都需要展示你已经尝试过的东西。该问题显示了相同的错误以及解决方案:在图像的副本上绘图 (img = new Bitmap(img); newGraphics = Graphics.FromImage(img);)。如果您在网上搜索您的错误消息,您会发现这个问题,这是您所期望的。因此,请尝试了解该问题和答案中的内容,然后如果仍然无法解决问题,请使用您尝试过的内容更新您的问题。
  • 很抱歉,该解决方案不起作用..我使用文件流并使用该解决方案会出错
  • 这就是我所说的“尝试理解该问题和答案中所说的内容”。使用文件流并不重要,因为Image.FromStream() 返回Image。然后,您可以创建一个new Bitmap(Image) 并创建一个Graphics 对象以在该位图上绘图。正如我链接的问题和答案中所解释的那样。

标签: c# watermark


【解决方案1】:

“不能从具有索引像素格式的图像创建图形对象。”不言自明。 GIF 图像是基于调色板的,您的 TIFF 图像似乎也被索引(虽然有全彩 TIFF)。

【讨论】:

  • 是的,我想这是正在发生的事情..任何解决方案?? @Nickolay Olshevsky
  • 您可以使用 Image.Save 方法将其转换为 png/jpg msdn.microsoft.com/en-us/library/ms142147.aspx 并在此之后添加水印。
  • 问题是它没有被加载到图形对象中......我想保持文件扩展名相同......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2011-09-28
  • 1970-01-01
  • 2012-07-26
相关资源
最近更新 更多