Renders a bitmap over another bitmap, with a specific alpha value. This can be used to overlay a logo or watermark over a bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Renders a bitmap over another bitmap, with a specific alpha value. This can be used to overlay a logo or watermark over a bitmap

Demo Code


using System.Net;
using System.IO;//from   ww w . java2  s . com
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System;

public class Main{
        public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, ImageCornerEnum corner) {
         return OverlayBitmap(destBmp, bmpToOverlay, 0, corner);
      }
        public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, Point overlayPoint) {
         return OverlayBitmap(destBmp, bmpToOverlay, 0, overlayPoint);
      }
        /// <summary>
      /// Renders a bitmap over another bitmap, with a specific alpha value.
      /// This can be used to overlay a logo or watermark over a bitmap
      /// </summary>
      /// <param name="destBmp">Bitmap over which image is to be overlaid</param>
      /// <param name="bmpToOverlay">Bitmap to overlay</param>
      /// <param name="overlayAlpha">Alpha value fo overlay bitmap.  0 = fully transparent, 100 = fully opaque</param>
      /// <param name="corner">Corner of destination bitmap to place overlay bitmap</param>
      /// <returns></returns>
      public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, int overlayAlpha, ImageCornerEnum corner) {
         //Translate corner to rectangle and pass through to other impl
         Point overlayPoint;

         if (corner.Equals(ImageCornerEnum.TopLeft)) {
            overlayPoint = new Point(0, 0);
         } else if (corner.Equals(ImageCornerEnum.TopRight)) {
            overlayPoint = new Point(destBmp.Size.Width -  bmpToOverlay.Size.Width, 0);
         } else if (corner.Equals(ImageCornerEnum.BottomRight)) {
            overlayPoint = new Point(destBmp.Size.Width - bmpToOverlay.Size.Width,
                               destBmp.Size.Height - bmpToOverlay.Size.Height);
         } else if (corner.Equals(ImageCornerEnum.Center)) {
            overlayPoint = new Point(destBmp.Size.Width / 2 - bmpToOverlay.Size.Width / 2,
                               destBmp.Size.Height / 2 - bmpToOverlay.Size.Height / 2);
         } else {
            overlayPoint = new Point(0,
                               destBmp.Size.Height - bmpToOverlay.Size.Height);
         }

         return OverlayBitmap(destBmp, bmpToOverlay, overlayAlpha, overlayPoint);
      }
        /// <summary>
      /// Renders a bitmap over another bitmap, with a specific alpha value.
      /// This can be used to overlay a logo or watermark over a bitmap
      /// </summary>
      /// <param name="destBmp">Bitmap over which image is to be overlaid</param>
      /// <param name="bmpToOverlay">Bitmap to overlay</param>
      /// <param name="overlayAlpha">Alpha value fo overlay bitmap.  0 = fully transparent, 100 = fully opaque</param>
      /// <param name="overlayPoint">Location in destination bitmap where overlay image will be placed</param>
      /// <returns></returns>
      public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, int overlayAlpha, Point overlayPoint) {
         //Convert alpha to a 0..1 scale
         float overlayAlphaFloat = (float)overlayAlpha / 100.0f;

         //Copy the destination bitmap
         //NOTE: Can't clone here, because if destBmp is indexed instead of just RGB, 
         //Graphics.FromImage will fail
         Bitmap newBmp = new Bitmap(destBmp.Size.Width,
                              destBmp.Size.Height);

         //Create a graphics object attached to the bitmap
         Graphics newBmpGraphics = Graphics.FromImage(newBmp);

         //Draw the input bitmap into this new graphics object
         newBmpGraphics.DrawImage(destBmp,
                            new Rectangle(0, 0, 
                                       destBmp.Size.Width,
                                       destBmp.Size.Height),
                            0, 0, destBmp.Size.Width, destBmp.Size.Height, 
                            GraphicsUnit.Pixel);

         //Create a new bitmap object the same size as the overlay bitmap
         Bitmap overlayBmp = new Bitmap(bmpToOverlay.Size.Width, bmpToOverlay.Size.Height);

         //Make overlayBmp transparent
         overlayBmp.MakeTransparent(overlayBmp.GetPixel(0,0));

         //Create a graphics object attached to the bitmap
         Graphics overlayBmpGraphics = Graphics.FromImage(overlayBmp);

         //Create a color matrix which will be applied to the overlay bitmap
         //to modify the alpha of the entire image
         float[][] colorMatrixItems = {
            new float[] {1, 0, 0, 0, 0},
               new float[] {0, 1, 0, 0, 0},
               new float[] {0, 0, 1, 0, 0},
               new float[] {0, 0, 0, overlayAlphaFloat, 0}, 
               new float[] {0, 0, 0, 0, 1}
         };

         ColorMatrix colorMatrix = new ColorMatrix(colorMatrixItems);

         //Create an ImageAttributes class to contain a color matrix attribute
         ImageAttributes imageAttrs = new ImageAttributes();
         imageAttrs.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

         //Draw the overlay bitmap into the graphics object, applying the image attributes
         //which includes the reduced alpha
         Rectangle drawRect = new Rectangle(0, 0, bmpToOverlay.Size.Width, bmpToOverlay.Size.Height);
         overlayBmpGraphics.DrawImage(bmpToOverlay,
                               drawRect,
                               0, 0, bmpToOverlay.Size.Width, bmpToOverlay.Size.Height,
                               GraphicsUnit.Pixel,
                               imageAttrs);
         overlayBmpGraphics.Dispose();

         //overlayBmp now contains bmpToOverlay w/ the alpha applied.
         //Draw it onto the target graphics object
         //Note that pixel units must be specified to ensure the framework doesn't attempt
         //to compensate for varying horizontal resolutions in images by resizing; in this case,
         //that's the opposite of what we want.
         newBmpGraphics.DrawImage(overlayBmp, 
                            new Rectangle(overlayPoint.X, overlayPoint.Y, bmpToOverlay.Width, bmpToOverlay.Height),
                            drawRect,
                            GraphicsUnit.Pixel);

         newBmpGraphics.Dispose();

         //Recall that newBmp was created as a memory bitmap; convert it to the format
         //of the input bitmap
         return ConvertBitmap(newBmp, destBmp.RawFormat);;
      }
          /// <summary>Converts a bitmap to another bitmap format, returning the new converted
      ///     bitmap
      /// </summary>
      /// 
      /// <param name="inputBmp">Bitmap to convert</param>
      /// <param name="destFormat">Bitmap format to convert to</param>
      /// 
      /// <returns>A new bitmap object containing the input bitmap converted.
      ///     If the destination format and the target format are the same, returns
      ///     a clone of the destination bitmap.</returns>
      public static Bitmap ConvertBitmap(Bitmap inputBmp, System.Drawing.Imaging.ImageFormat destFormat) {
         //If the dest format matches the source format and quality/bpp not changing, just clone
         if (inputBmp.RawFormat.Equals(destFormat)) {
            return(Bitmap)inputBmp.Clone();
         }

         //Create an in-memory stream which will be used to save
         //the converted image
         System.IO.Stream imgStream = new System.IO.MemoryStream();

         //Save the bitmap out to the memory stream, using the format indicated by the caller
         inputBmp.Save(imgStream, destFormat);

         //At this point, imgStream contains the binary form of the
         //bitmap in the target format.  All that remains is to load it
         //into a new bitmap object
         Bitmap destBitmap = new Bitmap(imgStream);

         //Free the stream
         //imgStream.Close();
         //For some reason, the above causes unhandled GDI+ exceptions
         //when destBitmap.Save is called.  Perhaps the bitmap object reads
         //from the stream asynchronously?

         return destBitmap;
      }
        /// <summary>Converts a bitmap to another bitmap format, returning the new converted
      ///     bitmap
      /// </summary>
      /// 
      /// <param name="inputBmp">Bitmap to convert</param>
      /// <param name="destMimeType">MIME type of format to convert to</param>
      /// 
      /// <returns>A new bitmap object containing the input bitmap converted.
      ///     If the destination format and the target format are the same, returns
      ///     a clone of the destination bitmap.</returns>
      public static Bitmap ConvertBitmap(Bitmap inputBmp, String destMimeType) {
         return ConvertBitmap(inputBmp, ImageFormatFromMimeType(destMimeType));
      }
}

Related Tutorials