在位图周围绘制边框 [英] Draw border around bitmap

查看:100
本文介绍了在位图周围绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个System.Drawing.Bitmap.

宽度是固定的,高度是变化的.

The width is fix, the height varies.

我想做的是在位图周围的所有4个边缘上添加一个白色边框,大约20个像素.

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

这将如何工作?

推荐答案

您可以在位图后面绘制一个矩形.矩形的宽度为(Bitmap.Width + BorderWidth * 2),位置为(Bitmap.Position-new Point(BorderWidth,BorderWidth)).或者至少这就是我要解决的方法.

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.

这是一些实际的源代码,说明了如何实现它(如果您要使用专用的方法来绘制图像):

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

这篇关于在位图周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆