steven
steven

Reputation: 393

resize image with white or black border

for resizing my picture i use this method:

private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
    Bitmap result = new Bitmap(nWidth, nHeight);
    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
        g.DrawImage(b, 0, 0, nWidth, nHeight);
    return result;
}

is there any possibilty to create a white or black border at the top/bottom or the right/left side of the picture, so that the picture is centered and i don't have to do it by css?

example: i have a picture that is 200x100 pixels and i want to have it to work in a 100x100 px image field on my website. at the moment i resize the 200x100 px picture to 100x50 pixels, so that it matches in the 100x100 px box and center the picture by css.

what i need is after resizing my picture to add a border to it, so that the picture is not 100x50 px, but 100x100 px with white or black border...

any ideas... can i do this with normal .net libraries? thanks for all advices!

best regards, jessica

Upvotes: 0

Views: 1205

Answers (1)

steven
steven

Reputation: 393

i finally found a way solving my problem:

private Bitmap ResizeBitmapWithPadding(Bitmap b, int nWidth, int nHeight, int originalWidth, int originalHeight)
{
    Bitmap result = new Bitmap(originalWidth, originalHeight);
    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
    { 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.Clear(GetColor());
        g.DrawImage(b, (originalWidth - nWidth) / 2, (originalHeight - nHeight) / 2, nWidnHeight);
    }
    return result;
}

thanks for your help!

Upvotes: 1

Related Questions