0

I create program like a paint. Now a trying create standard shapes. But I have got problem with drawing rectangle. I use method DrawCore for creating custom shapes on InkCanvas.

code:

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 brush = new SolidColorBrush(color);
        System.Windows.Media.Pen pen = new System.Windows.Media.Pen(new SolidColorBrush(color), 1);

        brush.Freeze();


        drawingContext.DrawRectangle(null, pen, new Rect(GetTheLeftTopPoint(), GetTheRightBottomPoint()));

        }
}

    System.Windows.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();
    }

    System.Windows.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();
    }

But my problem is, that I can draw not all directions. that is: enter image description here

How do it fix?

Thank you.

Naomiss
  • 167
  • 4
  • 14
  • 1
    You'll need to look at x and y values separately. Don't pick a whole point if its x or y values are better than the current point, only pick its x value if it's better than the current x, and only pick its y value if that's better than the current y. – Pieter Witvoet Jan 20 '17 at 12:03
  • @PieterWitvoet, Thank you! I just replace methods by this `drawingContext.DrawRectangle(brush, pen, new Rect(new Point(sp.X, sp.Y), new Point(stp.X, stp.Y)));` and now all working is correctly. – Naomiss Jan 20 '17 at 12:40

0 Answers0