如何在C#中为TIFF图像添加水印

时间:2012-10-12 18:06:06

标签: c# tiff watermark

我正在尝试打开TIFF图像,添加水印并将其保存为完全相同的TIFF格式。我的代码适用于PNG,但它无法打开和操作TIFF文件。我尝试使用TiffBitmapDecoder,但我无法使用它。我一直工作了16个小时没有运气。

string fileName = "sample.tiff";
int opacity = 127;
int X, Y;
string text = "My Sample Watermark";
Font font = new Font("Verdana", 18.0f);
Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);   

// I need to do something here to convert the stream so it can used by the Image object.

Image image = Image.FromStream(stream);               
Graphics g = Graphics.FromImage(image);
Brush myBrush = new SolidBrush(Color.FromArgb(opacity, Color.SteelBlue));
SizeF sz = g.MeasureString(text, font);
X = (int)(image.Width - sz.Width) / 2;
Y = (int)(image.Height - sz.Height);
g.DrawString(text, font, myBrush, new Point(X, Y));
stream.Dispose();
File.Delete(fileName);

// I need to do something here to save the Image in TIFF format with the same exact characteristics. 

image.Save(fileName);

我正在从电影文件中提取原始TIFF。现在我正在使用“Imagemagick转换CLI”来添加水印,但我想使用VB C#或者不必调用命令行来执行此操作。

1 个答案:

答案 0 :(得分:0)

你可以尝试将TIFF转换为png,然后再使用

返回
System.Drawing.Bitmap.FromFile(filename).Save(filename + ".png", System.Drawing.Imaging.ImageFormat.Png);

然后转换回来

System.Drawing.Bitmap.FromFile(filename + ".png").Save(filename, System.Drawing.Imaging.ImageFormat.Png);