Yago
Yago

Reputation: 454

How to add a watermark to a TIFF image in C#

I'm trying to open a TIFF image, add a Watermark and save it back to the exact same TIFF format. My code works great with PNGs but it was not able to open and manipulate a TIFF file. I tried using TiffBitmapDecoder but I can't get it to work. I been working for 16 hours with no luck.

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);

I'm extracting the original TIFF from a movie file. Right now I'm using "Imagemagick convert CLI" to add the watermark but I'll like to do that using VB C# or not having to invoke the command line to do that.

Upvotes: 0

Views: 2330

Answers (1)

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

You could try converting the TIFF to a png and then back again using

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

and then to convert back

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

Upvotes: 1

Related Questions