Question

I have a small bitmap image and I want to copy this image to a bigger space.
But I want to keep the measures from the original image and add a black frame around.
And centered in the middle.

    Image i = Image.FromFile(fileName); // This is 300x300
    Bitmap b = new Bitmap(500, 500);

    using(Graphics g = Graphics.FromImage(b))
    {
        g.DrawImage(i, 0, 0, 500, 500);
    }

but don't do what i want. This make the image bigger

example: original: 300x300 bigger: 400x400 with a frame all around with 50

SOLUTION: Ok so i did like this:

Image i = Image.FromFile(fileName); // This is 300x300
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    g.DrawImage(i, (500 - 300)/2,(500-300)/2);
}
Was it helpful?

Solution

There is a few way to do this.

  1. Have blank image with black background color and then just write your original image to it, basically merging your image to black blank template. That is where your script will come handy. This way you can always resize black template to desired size without changing main image.

  2. In web page create div space with CSS with black background and center your image into that div with properly set padding/margins to top,bottom

Those are from the top of my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top