Search code examples
c#windows-phone-7writeablebitmapdrawrectanglewriteablebitmapex

WriteableBitmapEx.DrawRectangle Thickness?


How do you change the thickness/weight for the rectangle outline drawn with the WriteableBitmapEx.DrawRectangle extension method? The code I use to draw the rectangle is:

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

Using this code, the thickness of the rectangle drawn in 1px.


Solution

  • Workaround from WritableBitmapEx.Add Thickness param for shapes

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