Answered how to fill color to the rectangle stroke ?

  • Wednesday, June 02, 2010 8:32 AM
     
      Has Code

    hi every body!

    i have a rectangle stroke and i wanna fill it! how? here is the code:

    StylusPointCollection pts = new StylusPointCollection();
    
    pts.Add(new StylusPoint(mouseLeftDownPoint.X, mouseLeftDownPoint.Y));
    
    pts.Add(new StylusPoint(mouseLeftDownPoint.X, currentPoint.Y));
    
    pts.Add(new StylusPoint(currentPoint.X, currentPoint.Y));
    
    pts.Add(new StylusPoint(currentPoint.X, mouseLeftDownPoint.Y));
    
    pts.Add(new StylusPoint(mouseLeftDownPoint.X, mouseLeftDownPoint.Y));
    
    if (st != null)
      inkCanvas.Strokes.Remove(st);
    
    st = new Stroke(pts);
    
    
    st.DrawingAttributes.Color = Colors.Red;
    inkCanvas.Strokes.Add(st);
    thank u!

All Replies

  • Wednesday, June 02, 2010 9:00 AM
     
     
  • Wednesday, June 02, 2010 6:45 PM
     
     
    thank you amir but my problem is not about rectangle! i am using stroke because i wanna have complex shapes. i wanna create shapes just like win 7 paint, star, clouds, .....! you got that? but i know my question was not clear enough. sorry.
  • Wednesday, June 02, 2010 8:58 PM
     
     
    shouldnt you use polyline or path instead of strokes?
    Kenneth
  • Thursday, June 03, 2010 2:13 AM
     
     Answered

    hi guy!

    I think this isn't a better way to build complex sharp by rect stroke, so I suggest using Path,  whatever complex sharp you can finnish by Path control!

    hope that help you !

    • Marked As Answer by moosavi.amir Friday, June 04, 2010 1:05 PM
    •  
  • Thursday, June 03, 2010 5:35 AM
     
     

    hi 4k.hrh!

    I checked out path before but the problem is when you select it and resize it just the path control will resize, and the shape in there will not resize! i wonder if you had any solution about path!

    thank you all guys!

  • Thursday, June 03, 2010 10:36 AM
    Moderator
     
     Answered Has Code

    Hi moosavi,

    If you do not want to use Path or other shapes, I suggest you can create a custom Stroke inherits from Stroke class and override the DrawCore method. Below is my Stroke class which can draw a Rectangle filled by brush.

     public class customStroke : Stroke
     {
      public customStroke(StylusPointCollection pts)
       : base(pts)
      {
       this.StylusPoints = pts;
      }
    
      protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
      {
       if (drawingContext == null)
       {
        throw new ArgumentNullException("drawingContext");
       }
       if (null == drawingAttributes)
       {
        throw new ArgumentNullException("drawingAttributes");
       }
       DrawingAttributes originalDa = drawingAttributes.Clone();
       SolidColorBrush brush2 = new SolidColorBrush(drawingAttributes.Color);
       brush2.Freeze();
       drawingContext.DrawRectangle(brush2, null, new Rect(GetTheLeftTopPoint(), GetTheRightBottomPoint()));
      }
    
      Point GetTheLeftTopPoint()
      {
       if (this.StylusPoints == null)
        throw new ArgumentNullException("StylusPoints");
       StylusPoint tmpPoint=new StylusPoint(double.MaxValue,double.MaxValue);
       foreach (StylusPoint point in this.StylusPoints)
       {
        if ((point.X < tmpPoint.X) || (point.Y < tmpPoint.Y))
         tmpPoint = point;
       }
       return tmpPoint.ToPoint();
      }
    
      Point GetTheRightBottomPoint()
      {
       if (this.StylusPoints == null)
        throw new ArgumentNullException("StylusPoints");
       StylusPoint tmpPoint = new StylusPoint(0, 0);
       foreach (StylusPoint point in this.StylusPoints)
       {
        if ((point.X > tmpPoint.X) || (point.Y > tmpPoint.Y))
         tmpPoint = point;
       }
       return tmpPoint.ToPoint();
      }
     }
    

     

    Hope this helps.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked As Answer by moosavi.amir Friday, June 04, 2010 1:05 PM
    •  
  • Friday, June 04, 2010 1:10 PM
     
     

    your right!

    but i have a problem with path witch if it solve path is really perfect for me!

    i wanted add a path that just contain one object and when i select and resize it the inside object resize. but now the path will resize and the child object have position in there and will not resize!
  • Friday, June 04, 2010 1:51 PM
     
     Answered Has Code

    i found it!

    path is the best answer!

    myPath.Stretch = Stretch.Fill;

    with this property i have a path object that will resize perfectly!

    thank you all guys!

     

    • Marked As Answer by moosavi.amir Friday, June 04, 2010 1:51 PM
    •  
  • Thursday, February 17, 2011 10:14 AM
     
     

    Hi moosavi.amir

                     I met the same problem as you ,  i appreciate it if you give your dome to me ...

                 

    Sincerely

                                     elegantluo

  • Thursday, February 17, 2011 10:18 AM
     
     

    Hi Bob Bao ,

                   i am  a newer in wpf , but i meet the same problem  above ,could you give a dome .

    thank you

    Sincerely

                         elegantluo

  • Tuesday, June 14, 2011 10:24 AM
    Moderator
     
     
    DEMO is here: http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110614%5E_CustomStrokeForInkCanvas.zip
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Wednesday, May 23, 2012 8:54 AM
     
     

    Dear All

    I've downloaded the above demo app. Thanks for the demo.

    After I draw the rectangle in the Inkcanvas with the above code and try to erase it with the code

    this.myInkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; ( "myInkCanvas" is the name of my InkCanvas)

    Erase functionality is not happening as expected. I want to erase just the points where my eraser moves.

    Any help please.

    Thanks


    Murali Mahendra Banala