Advertisement
scwagner

C# code to overlay one image over another

Jul 28th, 2011
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Drawing.Drawing2D;
  7.  
  8.  
  9.         public static Bitmap OverlayImage(Image background, Image foreground, Point foregroundLocation, Size foregroundSize, float opacity, bool resizeBackground)
  10.         {
  11.             Bitmap returnValue = null;
  12.             Graphics returnValueGraphics = null;
  13.             int backgroundHeight = background.Height;
  14.             Point backgroundLocation = new Point(0, 0);
  15.             int backgroundWidth = background.Width;
  16.             int foregroundHeight = foreground.Height;
  17.             int foregroundWidth = foreground.Width;
  18.             int newHeight = 0;
  19.             int newWidth = 0;
  20.  
  21.             if (false == resizeBackground)
  22.             {
  23.                 newHeight = backgroundHeight;
  24.                 newWidth = backgroundWidth;
  25.             }
  26.             else
  27.             {
  28.                 if (foregroundLocation.X < 0)
  29.                 {
  30.                     backgroundLocation = new Point(Math.Abs(foregroundLocation.X), backgroundLocation.Y);
  31.                     foregroundLocation = new Point(0, foregroundLocation.Y);
  32.                 }
  33.                 if (foregroundLocation.Y < 0)
  34.                 {
  35.                     backgroundLocation = new Point(backgroundLocation.X, Math.Abs(foregroundLocation.Y));
  36.                     foregroundLocation = new Point(foregroundLocation.X, 0);
  37.                 }
  38.                 newHeight = Math.Max((backgroundLocation.Y + backgroundHeight), (foregroundLocation.Y + Math.Min (foregroundHeight, foregroundSize.Height)));
  39.                 newWidth = Math.Max((backgroundLocation.X + backgroundWidth), (foregroundLocation.X + Math.Min (foregroundWidth, foregroundSize.Width)));
  40.             }
  41.             returnValue = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
  42.             returnValue.SetResolution(background.HorizontalResolution, background.VerticalResolution);
  43.             returnValueGraphics = Graphics.FromImage(returnValue);
  44.             returnValueGraphics.SmoothingMode = SmoothingMode.AntiAlias;
  45.             returnValueGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  46.             returnValueGraphics.DrawImage(
  47.                 background
  48.             , new Rectangle(backgroundLocation, background.Size)
  49.             , 0
  50.             , 0
  51.             , backgroundWidth
  52.             , backgroundHeight
  53.             , GraphicsUnit.Pixel
  54.             );
  55.  
  56.             ImageAttributes imageAttributes = new ImageAttributes();
  57.             float[][] colorMatrixElements = {
  58.                                                 new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},      
  59.                                                 new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},        
  60.                                                 new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},        
  61.                                                 new float[] {0.0f,  0.0f,  0.0f,  opacity, 0.0f},        
  62.                                                 new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
  63.             ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  64.             imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  65.             returnValueGraphics.DrawImage(
  66.                 foreground
  67.             , new Rectangle(foregroundLocation, foregroundSize)
  68.             , 0
  69.             , 0
  70.             , foreground.Width
  71.             , foreground.Height
  72.             , GraphicsUnit.Pixel
  73.             , imageAttributes
  74.             );
  75.             returnValueGraphics.Dispose();
  76.  
  77.             return (returnValue);
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement