Search code examples
asp.netimageimage-processingwatermark

ASP.NET: Adding 'Watermark' to images on the fly


I have seen great questions and answers regarding adding watermark on images with php

I would like to do the same, this time with ASP.NET

So here are a couple of questions.

  1. How can i do that with ASP?
  2. Is this process going to be a great overload for the server?
  3. Could i use an image for a watermark instead of simple text?

Solution

  • Here is one more example http://www.codeproject.com/KB/web-image/ASPImaging1.aspx from codeproject that you can do many thinks on the image, including adding watermark from image.

    I think that this process is take cpu power ether is on php, ether on asp.net. So an image cache schema is a must for this kind of works.

    Here is some basic code. In this code you have to change the position of the watermark, and the size of the images. The watermark can be a png image with tranparent.

        public void MakePhoto(...parametres...)
        {
            Bitmap outputImage = null;
            Graphics g = null;
    
            try
            {                
                // the final image
                outputImage = new Bitmap(OutWidth, OutHeight, PixelFormat.Format24bppRgb);
    
                g = Graphics.FromImage(outputImage);
                g.CompositingMode = CompositingMode.SourceCopy;
                Rectangle destRect = new Rectangle(0, 0, OutWidth, OutHeight);
    
                // the photo
                using (var BasicPhoto = new Bitmap(cBasicPhotoFileOnDisk))
                {
                    g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel);
                }
    
                g.CompositingMode = CompositingMode.SourceOver;
                // the watermark
                using (var WaterMark = new Bitmap(cWaterMarkPhotoOnDisk))
                {
                    Rectangle destWaterRect = new Rectangle(0, 0, OutWidth, OutHeight);
    
                    g.DrawImage(WaterMark, destWaterRect, 0, 0, OutWidth, OutHeight, GraphicsUnit.Pixel);
                }
    
                outputImage.Save(TheFileNameTosaveIt, ImageFormat.Jpeg);
    
            }
            catch (Exception x)
            {
                Debug.Assert(false);
                ... log your error, and send an error image....                
            }
            finally
            {
                if (outputImage != null)
                    outputImage.Dispose();
    
                if (g != null)
                    g.Dispose();
            }
        }
    

    If you wish to make a custom handle the above code is stands, but you change the save line only. Something like.

    public void ProcessRequest (HttpContext context)    
    {
        context.Response.ContentType = "image/jpeg";
    
        // add you cache here
        context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(200));
        context.Response.Cache.SetMaxAge(new TimeSpan(0, 200, 0));
        context.Response.BufferOutput = false;
    
    
        ..... the above code....
        outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        ..... the above code....
    
    
        context.Response.End();
    }