Friday, November 2, 2007

Watermark Image

·

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Image;

public void AddWatermark(string filename, string watermarkText, Stream outputStream) {
Bitmap bitmap = Bitmap.FromFile(filename);
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.FromArgb(10, 0, 0, 0); //Adds a black watermark with a low alpha value (almost transparent).
Point atPoint = new Point(100, 100); //The pixel point to draw the watermark at (this example puts it at 100, 100 (x, y)).
SolidBrush brush = new SolidBrush(color);

Graphics graphics = null;
try {
graphics = Graphics.FromImage(bitmap);
} catch {
Bitmap temp = bitmap;
bitmap = new Bitmap(bitmap.Width, bitmap.Height);
graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(temp, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
temp.Dispose();
}

graphics.DrawString(text, font, brush, atPoint);
graphics.Dispose();

bitmap.Save(outputStream);
}

To call it, just add something like this to your aspx file (assume the class is called MyImage):

string filename = Request.MapPath(Request.QueryString["filename"]);
MyImage image = new MyImage();
image.AddWatermark(filename, "Watermark Test", HttpResponse.OutputStream);

0 comments: