Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw border around bitmap

I have got a System.Drawing.Bitmap in my code.

The width is fix, the height varies.

What I want to do, is to add a white border around the bitmap, with about 20 pixel, to all 4 edges.

How would this work?

like image 541
abc Avatar asked Nov 13 '12 07:11

abc


1 Answers

You could draw a rectangle behind the bitmap. The width of the rectangle would be (Bitmap.Width + BorderWidth * 2), and the position would be (Bitmap.Position - new Point(BorderWidth, BorderWidth)). Or at least that's the way I'd go about it.

EDIT: Here is some actual source code explaining how to implement it (if you were to have a dedicated method to draw an image):

private void DrawBitmapWithBorder(Bitmap bmp, Point pos, Graphics g) {
    const int borderSize = 20;

    using (Brush border = new SolidBrush(Color.White /* Change it to whichever color you want. */)) {
        g.FillRectangle(border, pos.X - borderSize, pos.Y - borderSize, 
            bmp.Width + borderSize, bmp.Height + borderSize);
    }

    g.DrawImage(bmp, pos);
}
like image 101
antonijn Avatar answered Sep 29 '22 19:09

antonijn