Most Efficient Way To Watermark Image C# On The Fly?

leen3o picture leen3o · Dec 2, 2010 · Viewed 13.8k times · Source

I have an ecommerce store built in asp.net c# (Webforms) and a lot of the new product images are very hard to source, so I'd like to watermark them with our logo or domain name.

There are too many products to just download the images and add the watermark, and users with limited image editing experience will be uploading new ones (So they won't have a clue how to add the watermark).

So I guess this just leaves me with using a HttpHandler? Yes / No? If so can you provide some insight (Preferably code samples in C#) into the most efficient way of adding the watermark, considering some pages will have around 20 images (Jpegs) on (All of which need to be watermarked)

Answer

Øyvind Bråthen picture Øyvind Bråthen · Dec 2, 2010

I would obtain the Graphicsobject to the jpeg, and then draw the watermark on top of that item, and save it again with the watermark:

using (Image image = Image.FromFile("myImage.jpg"))
using(Graphics g = Graphics.FromImage( image)){
  g.DrawImage( myWaterMarkImage, myPosition);
  image.Save(myFilename);
}