c# - How to increase thickness of shape in WPF -


i'm trying create paint application in wpf using canvas. want increase thickness of shape while drawing try increasing strokethickness.

this how want be:

enter image description here

and get:

enter image description here

as can see outline extends inside boundary. how can make extends on both side?

here code:

in mousedown event:

 rectangle rect = new rectangle();  rect.stroke = _color;  rect.strokethickness = _size;  canvas.setleft(rect, _startpoint.x);  canvas.settop(rect, _startpoint.y);  cv_paintboard.children.add(rect);  isdrawing = true; 

in mousemove event:

if (isdrawing == true && e.leftbutton == mousebuttonstate.pressed) {     canvas canvas = (canvas)sender;      rectangle rect = canvas.children.oftype<rectangle>().lastordefault();      if (rect != null)     {         point endpoint = e.getposition((iinputelement)sender);         point startpoint = new point(             math.min(endpoint.x, _startpoint.x),             math.min(endpoint.y, _startpoint.y)             );          rect.width = math.max(endpoint.x, _startpoint.x) - startpoint.x;         rect.height = math.max(endpoint.y, _startpoint.y) - startpoint.y;          canvas.setleft(rect, startpoint.x);         canvas.settop(rect, startpoint.y);     } } 

you need update width/height , left/top specific needs.

the below example demonstrates needs. if ok you, let me know.

mainwindow.xaml

<window x:class="wpfdrawing.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="447.368" width="606.579"> <grid>     <grid.rowdefinitions>         <rowdefinition height="13*"/>         <rowdefinition height="7*"/>     </grid.rowdefinitions>     <canvas grid.rowspan="1">         <rectangle x:name="rect1" fill="transparent" horizontalalignment="left" height="70" canvas.left="248" canvas.top="104" stroke="black" width="94" opacity="0.5" />         <rectangle x:name="rect2" fill="#ff52e03c" horizontalalignment="left" height="70" canvas.left="248" canvas.top="104" stroke="black" width="94" opacity="0.5" />      </canvas>     <textbox x:name="tbthickness" horizontalalignment="left" height="23" margin="84,27,0,0" grid.row="1" textwrapping="wrap" text="5" verticalalignment="top" width="120"/>     <button content="button" horizontalalignment="left" margin="237,28,0,0" grid.row="1" verticalalignment="top" width="75" click="button_click"/> </grid> </window> 

mainwindow.xaml.cs

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes;  namespace wpfdrawing {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         double designwidth;         double designheight;          double designleft, designtop;          public mainwindow()         {             initializecomponent();              designwidth = rect2.width;             designheight = rect2.height;              designleft = canvas.getleft(rect2);             designtop = canvas.gettop(rect2);         }                 private void button_click(object sender, routedeventargs e)         {             rect2.strokethickness = double.parse(tbthickness.text);              canvas.setleft(rect2,designleft - (rect2.strokethickness / 2));             canvas.settop(rect2, designtop - (rect2.strokethickness / 2));                                      rect2.width = designwidth + rect2.strokethickness;             rect2.height = designheight + rect2.strokethickness;          }     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -