我正在尝试使用 .png 图像为图像添加水印我的 .jpg 和 .png 图像正在正确添加水印,但 .tif 和 .gif 图像会出错。
我正在用 c# 做。我得到了这个错误:
{“无法从具有索引像素格式的图像创建图形对象。在 System.Drawing.Graphics.FromImage(Image image) at Watermarking.test_image_watermak.button1_Click(Object sender, 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);
对于这些类型的图像,您需要做些什么不同的事情???