zoukankan      html  css  js  c++  java
  • wpf轻量绘图DrawingVisual


    wpf内置提供了Ellipse等标签画图形,不仅如此而且还提供了许多的事件(因为其继承自FrameworkElement).
    在某些情况下,我们可以不采用这些标签

    仅仅用于呈现,并不复杂的操作(没有事件).

    DrawingVisual是一个轻量绘图类,在上述情况成立下可以采用DrawingVisual类来绘图提高性能.
    方法如下

    1.使用DrawingVisual必须创建一个容器(从FrameworkElement继承创建一个容器)

    public class MyVisualHost : FrameworkElement
    {
    }

    2.创建一个全局可视对象的集合(VisualCollection)
    private VisualCollection _children;

        
    public MyVisualHost()
        
    {
            _children 
    = new VisualCollection(this);
    }

    3.创建DrawingVisual
    先初始化一个DrawingVisual类,然后使用RenderOpen()获取其DrawingContext 对象(DrawingContext 不可以以new方式初始化),DrawingContext 提供了一些以Draw开头的绘图方法,绘制完成以后必须调用Close方法(不然不会呈现)
            private DrawingVisual CreateDrawingVisualRectangle()
            
    {
                DrawingVisual drawingVisual 
    = new DrawingVisual();

                
    // Retrieve the DrawingContext in order to create new drawing content.
                DrawingContext drawingContext = drawingVisual.RenderOpen();

                
    // Create a rectangle and draw it in the DrawingContext.
                Rect rect = new Rect(new Point(160100), new Size(32080));
                drawingContext.DrawRectangle(Brushes.LightBlue, (Pen)
    null, rect);

                
    // Persist the drawing content.
                drawingContext.Close();

                
    return drawingVisual;
            }

    DrawingContext还包含一些以Push开头的方法,可以为图形设置透明度,effect等。看sdk就ok了,记录下

    另外使用DrawingGroup也可以,不过容器变成了Image
     // Display the drawing using an image control.
                Image theImage = new Image();
                DrawingImage dImageSource 
    = new DrawingImage(dGroup);
                theImage.Source 
    = dImageSource;
                panel.Children.Add(theImage);

  • 相关阅读:
    高并发计算服务器数量
    Java子线程中操作主线程Private级别数据
    Java线程并发中常见的锁--自旋锁 偏向锁
    Java锁之自旋锁详解
    Java多线程:向线程传递参数的三种方法
    TPS、并发用户数、吞吐量关系
    C/C++中near和far的区别
    qt的pos()和globalpos()(globalpos是相对于桌面的)
    如何获得 Qt窗口部件在主窗口中的位置--确定鼠标是否在某一控件上与在控件上的位置
    QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1213375.html
Copyright © 2011-2022 走看看