4

如何更改使用 WriteableBitmapEx.DrawRectangle 扩展方法绘制的矩形轮廓的粗细/重量?我用来绘制矩形的代码是:

WriteableBitmap wbmp = new WriteableBitmap(bmp);
wbmp.DrawRectangle(0, 0, 480, 360, Colors.DarkGray);

使用这段代码,绘制的矩形的粗细为 1px。

4

3 回答 3

3

WritableBitmapEx 的解决方法。为形状添加厚度参数

//Original points for line
int x1 = (int)pts[0].X;
int y1 = (int)pts[0].Y;
int x2 = (int)pts[1].X;
int y2 = (int)pts[1].Y;

//Parallel line code from http://stackoverflow.com/questions/2825412/draw-a-parallel-line var L = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
var offsetPixels = 4;//Line "thickness"
// This is the second line will be parallel to the first
int x1p = (int)(x1 + offsetPixels * (y2 - y1) / L);
int x2p = (int)(x2 + offsetPixels * (y2 - y1) / L);
int y1p = (int)(y1 + offsetPixels * (x1 - x2) / L);
int y2p = (int)(y2 + offsetPixels * (x1 - x2) / L);

//writeableBmp.DrawLine(x1, y1, x2, y2, Colors.Red);
//writeableBmp.DrawLine(x1p, y1p, x2p, y2p, Colors.Blue);
//Create closed filled polygon for "thick line"
writeableBmp.FillPolygon(new int[] { x1, y1, x2, y2, x2p, y2p, x1p, y1p, x1, y1 }, Colors.Red);
于 2013-01-27T08:51:19.957 回答
0

这是一个很好的解决方法,使用已经来自 'Rectangle' 的鲜为人知的 'inflate' 方法:

int pen_thickness = 5;

Rectangle original_rect = new Rect(0, 0, 480, 360); // using the poster's original values

for(int i = 0; i < pen_thickness; i++)
{
    Rectangle bigger_rect = Rectangle.Inflate(original_rect, i, i);
    wbmp.DrawRectangle(Pens.DarkGray, bigger_rect);
}

这将创建一个从最初寻找的矩形扩展的矩形。也可以进行快速修改,使厚度向内扩展一半(膨胀为负值)和向外扩展一半(膨胀为正值)。

于 2014-01-27T04:30:49.320 回答
0

创建了以下...非常适合 WriteableBitmapEx

private static void DrawRectangle(WriteableBitmap bitmap, 
    int left, int top, int width, int height, Color color, int thinkness)
{
    var x1 = left;
    var y1 = top;
    var x2 = left + width;
    var y2 = top + height;

    bitmap.DrawRectangle(x1, y1, x2, y2, color);

    for (var i = 0; i < thinkness; i++)
    {
        bitmap.DrawRectangle(x1--, y1--, x2++, y2++, color);
    }
}
于 2017-02-13T04:24:37.527 回答