Adding a Rotated Watermark to Images on the fly

Hello folks,

A while ago, I demonstrated an example for resizing a transparent GIF image on the fly, now I will demonstrate how to resize and add a watermark at the same time. Please take a look at Resizing transparent GIF Images so you can understand this process.

I will show you 2 examples: a more elegant one and an ugly one, with a static class, that you can implement with your class. And another one using my class called ImageManipulator.

This algorithm uses OctreeQuantizer to save the image but this is optional. To download it click here

Before After
public sealed class ImageManipulator : IDisposable
{
    #region privare properties
    Byte[] ImageContent = null;
    Graphics g = null;
    Bitmap bitmap = null;
    OctreeQuantizer quantizer = null;
    #endregion

    #region public properties
    private MemoryStream imageResult = null;
    public MemoryStream ImageResult
    {
        get
        {
            if (imageResult == null)
                imageResult = new MemoryStream();

            return imageResult;
        }
        set
        {
            imageResult = value;
        }
    }
    #endregion

    #region constructors
    private ImageManipulator() { }

    public ImageManipulator(Byte[] ImageContent)
    {
        this.ImageContent = ImageContent;
        this.quantizer = new OctreeQuantizer(255, 8);
    }
    #endregion

    #region Dispose
    public void Dispose()
    {
        ImageContent = null;

        if (g != null)
            g.Dispose();
            
        if (bitmap != null)
            bitmap.Dispose();

        if (ImageResult != null)
            ImageResult.Dispose();
    }
    #endregion

    #region public Methods
    public void AddWatermark(string WatermarkText)
    {
        if (ImageContent == null || ImageContent.Length == 0)
            return;

        if (g == null)
        {
            if (ImageResult == null)
            {
                bitmap = new Bitmap(new MemoryStream(ImageContent));
            }
            else
            {
                bitmap = new Bitmap(ImageResult);
            }

            g = Graphics.FromImage(bitmap);

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.High;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            g.DrawImage(bitmap, 0, 0);
        }

        Font font = new Font("Arial", (bitmap.Width * 0.1f) + 2, FontStyle.Bold, GraphicsUnit.Pixel);
        SolidBrush brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); //intensity
        Matrix matrix = new Matrix();
        matrix.Rotate(-45, MatrixOrder.Append); //rotation angle
        g.Transform = matrix;
        g.DrawString(WatermarkText, font, brush, new Point((bitmap.Height / 2) * -1, bitmap.Height - (int)(bitmap.Height * 0.3f)));

        Bitmap quantized = quantizer.Quantize(bitmap);

        ImageResult = new MemoryStream(bitmap.Width * bitmap.Height);

        quantized.Save(ImageResult, ImageFormat.Png);

        quantized.Dispose();

    }

    /// <summary>
    /// Resize all kinds of bitmap, quantize and tranform it
    /// </summary>
    public void Resize(int Width, int Height)
    {
        if (ImageContent == null || ImageContent.Length == 0)
            return;

        ImageResult = new MemoryStream(ImageContent);

        if (Width == 0 || Height == 0)
            return;

        try
        {
            Image imageOriginal = Image.FromStream(ImageResult);

            bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb);

            if (g == null)
            {
                g = Graphics.FromImage(bitmap);

                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.High;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            }

            g.DrawImage(imageOriginal, new Rectangle(0, 0, Width, Height), 0, 0,
                imageOriginal.Width, imageOriginal.Height,
                GraphicsUnit.Pixel);

            imageOriginal.Dispose();

            Bitmap quantized = quantizer.Quantize(bitmap);

            ImageResult = new MemoryStream(Width * Height);

            quantized.Save(ImageResult, ImageFormat.Png);

            quantized.Dispose();

        }
        catch { }

    }
    #endregion
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s