zoukankan      html  css  js  c++  java
  • DrawingVisual和DrawingContext的简单应用(在Image上画图)

    1.新建一个继承Image的基类,因为要在Image对象上画图,所以下面的代码必须放入基类中,如果放在加载图片的主窗体上,背景会变成黑色。

      protected override Visual GetVisualChild(int index)
      {
                return visuals[index];
       }
       protected override int VisualChildrenCount
       {
            get
                {
                    return visuals.Count;
                }
        }

    下面是新建的基类代码

     public class DrawingImages : Image
        {
            public List<Visual> visuals = new List<Visual>();
    
            protected override Visual GetVisualChild(int index)
            {
                return this.visuals[index];
            }
    
            protected override int VisualChildrenCount
            {
                get
                {
                    return visuals.Count;
                }
            }
    
            public void AddVisual(Visual visual)
            {
                this.visuals.Add(visual);
    
                base.AddVisualChild(visual);
                base.AddLogicalChild(visual);
            }
    
            public void DeleteVisual(Visual visual)
            {
                this.visuals.Remove(visual);
    
                base.RemoveVisualChild(visual);
                base.RemoveLogicalChild(visual);
            }
    
            public void DeleteVisualAll()
            {
                foreach (var item in visuals)
                {
                    base.RemoveVisualChild(item);
                    base.RemoveLogicalChild(item);
                }
                this.visuals.Clear();
            }
        }
    View Code

    2.主窗体代码

     xaml窗体代码默认即可。后台代码如下:

     /// <summary>
        /// DrawingImage.xaml 的交互逻辑
        /// </summary>
        public partial class DrawingImage : Window
        {
            private DrawingImages img_ = new DrawingImages();
            public DrawingImage()
            {
                InitializeComponent();
                this.Content = img_;
                img_.Stretch = Stretch.Fill;
                img_.MouseLeftButtonDown += new MouseButtonEventHandler(img__MouseLeftButtonDown);
                img_.MouseMove += new MouseEventHandler(img__MouseMove);
                img_.MouseLeftButtonUp += new MouseButtonEventHandler(img__MouseLeftButtonUp);
                img_.MouseDown += new MouseButtonEventHandler(img__MouseDown);
                InitDrawImage();
               
            }
    
            void img__MouseDown(object sender, MouseButtonEventArgs e)
            {
                if (e.ChangedButton==MouseButton.Right)
                   img_.DeleteVisualAll();
            }
    
            private void InitDrawImage()
            {
                int X = (int)this.Width;
                int Y = (int)this.Height;
                DrawingVisual dv = new DrawingVisual();
                using (DrawingContext dc = dv.RenderOpen())
                {
                    Rect rect = new Rect(new Point(0, 0), new Size(X, Y));
                    Brush bc = new SolidColorBrush(Colors.LightBlue);
                    Pen pen = new Pen(Brushes.Pink, 1);
                    dc.DrawRectangle(bc, pen, rect);
                    dc.DrawText(new FormattedText("LightBlue", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("微软雅黑"), (int)Y / 32, Brushes.White), new Point((int)X / 25, (int)Y / 21));
                }
                var rtbitmap = new RenderTargetBitmap(X, Y, 0.0, 0.0, PixelFormats.Default);
                rtbitmap.Render(dv);
                img_.Source = rtbitmap;
            }
    
            private Point? _startPoint;
            private DrawingVisual selectionSquare;
            void img__MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                _startPoint = e.GetPosition(this.img_);
                selectionSquare = new DrawingVisual();
                this.img_.AddVisual(selectionSquare);
                this.img_.CaptureMouse();
     
            }
    
            private Brush selectionSquareBrush = Brushes.Transparent;
            private Pen selectionSquarePen = new Pen(Brushes.LightPink, 2);
            private void DrawSelectionSquare(Point point1, Point point2)
            {
                if (selectionSquare == null)
                    return;
                using (DrawingContext dc = selectionSquare.RenderOpen())
                {
                    dc.DrawRectangle(selectionSquareBrush, selectionSquarePen,
                        new Rect(point1, point2));
                }
            }
    
            void img__MouseMove(object sender, MouseEventArgs e)
            {
                if (_startPoint == null)
                    return;
                Point endPoint = e.GetPosition(this.img_);
                DrawSelectionSquare((Point)_startPoint, endPoint);
                
            }
    
            void img__MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                this.img_.ReleaseMouseCapture();
                _startPoint = null;
            }
          
        }
    View Code
  • 相关阅读:
    树莓派上使用docker部署aria2,minidlna
    在Ubuntu上部署一个基于webrtc的多人视频聊天服务
    解决.net core3.1使用docker部署在Ubuntu上连接sqlserver报error:35的问题
    .Net Core in Docker
    .Net Core in Docker
    SQL Server in Docker
    使用Docker运行SQL Server
    Spring Boot 2.x(四):整合Mybatis的四种方式
    Spring Boot 2.x (一):HelloWorld
    yarn (npm) 切换设置镜像源
  • 原文地址:https://www.cnblogs.com/smartsensor/p/3133666.html
Copyright © 2011-2022 走看看