WriteableBitmapEx.DrawRectangle拡張メソッドで描画された長方形の輪郭の太さ/太さをどのように変更しますか?長方形を描くために使用するコードは次のとおりです。
WriteableBitmap wbmp = new WriteableBitmap(bmp);
wbmp.DrawRectangle(0, 0, 480, 360, Colors.DarkGray);
このコードを使用して、1pxで描画された長方形の厚さ。
WriteableBitmapEx.DrawRectangle拡張メソッドで描画された長方形の輪郭の太さ/太さをどのように変更しますか?長方形を描くために使用するコードは次のとおりです。
WriteableBitmap wbmp = new WriteableBitmap(bmp);
wbmp.DrawRectangle(0, 0, 480, 360, Colors.DarkGray);
このコードを使用して、1pxで描画された長方形の厚さ。
シェイプの WritableBitmapEx.Add Thickness パラメータからの回避策
//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);
「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);
}
これにより、最初に求められた長方形から拡張された長方形が作成されます。厚さを半分内側 (膨張の負の値) と外側の半分 (膨張の正の値) に拡張するように、簡単な変更を行うこともできます。
以下を作成しました... 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);
}
}