Padding Images using GDI+

September 9, 2009 15:07 by Alec Bryte

Recently I had to poke around in the good ol’ WinForms code and add some spacing to the images posted onto a form. I wrote a simple helper method for adding that extra space (padding) around the image. Here it is:

 

public static Image PadImage(Image source, int left, int top, int right, int bottom)
{
    Bitmap bmp = new Bitmap(source.Width + left + right, source.Height + top + bottom);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source, left, top);
    g.Dispose();
    return bmp;
}