CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

c# Extension Method - OverlayImage

192 Views
Copy Code Show/Hide Line Numbers
   1:  public static Bitmap OverlayImage(this Bitmap background, Bitmap foreground, int widthBump, int heightBump)
   2:  {
   3:      try
   4:      {
   5:          Graphics myGraphic = null;
   6:          Bitmap imgB = background;
   7:          Bitmap imgF = foreground;
   8:          using (Bitmap m = new Bitmap(background))
   9:          {
  10:              myGraphic = Graphics.FromImage(m);
  11:              myGraphic.DrawImageUnscaled(imgB, 0, 0);
  12:              var x = ((imgB.Width - imgF.Width) / 2) + widthBump;
  13:              var y = ((imgB.Height - imgF.Height) / 2) + heightBump;
  14:              myGraphic.DrawImageUnscaled(imgF, x, y);
  15:              myGraphic.Save();
  16:              return new Bitmap(m);
  17:          }
  18:      }
  19:      catch
  20:      {
  21:          return foreground;
  22:      }
  23:  }
by Hal Lesesne
  October 02, 2009 @ 9:14am
Tags:
Description:
Reposting of previous to fix tags. Handy extension method on Bitmap to overlay another image.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate