Draw border around bitmap

后端 未结 3 1449
天涯浪人
天涯浪人 2021-01-12 21:10

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 bitma

3条回答
  •  孤街浪徒
    2021-01-12 21:35

    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);
    }
    

提交回复
热议问题