1
votes

Is there a quick way using System.Drawing to quickly enlarge the image canvas of an .png image? (see example below). The caveat is the background might be transparent and I want to keep it transparent.

Edit: Needs to be in ASP .Net CORE

Alternatively, is there a way of putting the image on a white background that is slightly larger than the image?

enter image description here

1

1 Answers

0
votes

After a few days of trial and error, I think I found something that works

Image overlayImage = //get your image here from file or url.
xloc = //x coord where to start overlay image.  
yloc = //y coord where to start overlay image.  
canvasWidth = //width of background canvas  
canvasHeight = //height of background canvas  
Bitmap baseImage = new Bitmap(canvasWidth, canvasHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

using (Graphics graphics = Graphics.FromImage(baseImage))
{
    using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White))
    {
        graphics.FillRectangle(myBrush, new Rectangle(0, 0, canvasWidth, canvasHeight));  // white rectangle

    }
    graphics.CompositingMode = CompositingMode.SourceOver;
    graphics.DrawImage(overlayImage, xloc, yloc);

} // graphics will be disposed at this line