locked
How to use WPF to implement multi-pen drawing? RRS feed

答案

全部回复

  •     public class Window2Panel : Panel
        {
            private VisualCollection _col;
    
            public Window2Panel()
            {
                _col = new VisualCollection(this); 
            }
    
            protected override Visual GetVisualChild(int index)
            {
                return _col[index];
            }
    
            protected override int VisualChildrenCount
            {
                get
                {
                    return _col.Count;
                }
            }
    
            bool down = false;
    
            CustomPen pen=null; 
            protected override void OnMouseDown(MouseButtonEventArgs e)
            {
                down = true;
                pen = new CustomPen(e.GetPosition(this));
                _col.Add(pen); 
            }
    
            protected override void OnMouseMove(MouseEventArgs e)
            {
                if (down && pen!=null)
                {
                    pen.AddPoint(e.GetPosition(this));  
                }
            }
            protected override void OnMouseUp(MouseButtonEventArgs e)
            {
                down = false;
                pen = null;
            }
        }
    
        public class CustomPen : DrawingVisual
        {
            private Stroke stroke;
    
            public CustomPen(Point point):this(new StylusPoint(point.X, point.Y))
            {            
            }
    
            public CustomPen(StylusPoint point)
            {
                StylusPointCollection points = new StylusPointCollection();
                points.Add(point); 
                stroke = new Stroke(points);
            }
    
            public void AddPoint(Point point)
            {
                this.AddPoint(new StylusPoint(point.X, point.Y));     
            }
    
            public void AddPoint(StylusPoint point)
            {
                stroke.StylusPoints.Add(point);
                this.Draw(); 
            }
    
            private void Draw()
            {
                DrawingContext dc = this.RenderOpen(); 
                stroke.Draw(dc);
                dc.Close(); 
            }
        }
    
    in the running, I  found a noticeable delay between the mouse point and the track, and using CPU resouces is very big 


    2011年9月29日 1:48
  • Hi,

    Welcome. For the performance, I will suggest you to test them by the WPF Performence tool. And Regards to te Custom InkCanvas with the custom DrawingVisual. I will try to consult it.

    Thanks,


    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.

    2011年9月29日 8:57
  • thank!
    2011年9月30日 1:51
  • InkCanvas no this Problem.
    2011年10月1日 6:20
  • But incanvas can not multi-pen drawing.
    2011年10月2日 10:18
  • I can't get your code fragment work in my enviroment. But i tested the sample code you referenced. It works fine in my machine. So please help to check if the sample code also works in your machine. Addtionally, please help to provide the sample code and check which line causes the CPU high using CLR profiler (http://www.microsoft.com/download/en/details.aspx?id=16273).

     

    Regards,
    Han Xia
    Microsoft Online Community Support

     

    2011年10月4日 8:45
  • Thank! Here are two examples can be run, using VS2008 and. Net FrameWork3.0。
    Delay example, the longer the draw, between the mouse and lines will be a very significant delay (after the mouse to stop, delay disappeared), and CPU utilization much, mainly because of the mouse each take a sampling point, we need to re-draw the lines;Non-delayed example  you will find that handwriting is very smooth, although take up memory, but not as good as the first clear, the key is not that obvious delay.
    My aim is to achieve multi-pen, refers to more than one person can use the stylus on the touch pad or finger free writing, which requires hardware support. Currently there is no hardware problem, the problem is a multi-pen when the delay is not significant (due to hardware limitations, there will be some delay in itself).
    Examples of the use of delayed implementation, to achieve multi-pen, but the process caused the delay with the hardware itself, and do not make people suffer; example if the non-delay multiple ways to achieve freedom of writing, then, is an ideal, but I can not find how to DynamicRenderer the RootVisual simultaneously display different handwriting method.
    What should I do here?  how achieve multi-pen? Are there other ways? Please help me!

    2011年10月6日 5:53
  • This problem is very complex?
    2011年10月8日 1:37
  • I still don't quite understand what you want. You said that the performance is slow in multi-pen case. But your sample shows a single stylus scenario. So does it mean that your problem still exist in single stylus case? Per your sample code, it's expected that the performance becomes more and more slow. Since you draw more and more points in the windows, WPF engine should cost much more time to draw them. You can not expect that they have the same performance when you draw 100 points or 100000 points. I didn't try but I think you can draw the points into a RenderTargetBitmap so that you don't need to draw the points again and again.

    Another thing is WPF3.0 doen't support multi-touch, so please update your .net framework to .net 4.0.

     

    Best Regards,

    Han Xia

    2011年10月8日 7:34
  • Thank you!I do not understand what you said to use RenderTargetBitmap to render content. Can write a simple example? Greatly appreciated!

     

    2011年10月8日 9:37
  • Some people can write about how to use RenderTargetBitmap to show the example?Thank!
    2011年10月11日 6:26
  • Can someone guide me? Thank!
    2011年10月12日 7:27
  • If you search internet,you can find many sample about RenderTargetBitmap. Further more, our MSDN also provide the sample code:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
    http://www.i-programmer.info/programming/wpf-workings/538-rendertargetbitmap-visual-vector-to-bitmap.html

     

    Regards,
    Han Xia
    Microsoft Online Community Support

    • 已标记为答案 张柱敏 2011年11月23日 5:45
    2011年11月18日 5:13