博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用Adorner制作用于图像裁切的选择框
阅读量:5888 次
发布时间:2019-06-19

本文共 4899 字,大约阅读时间需要 16 分钟。

原文:

前天,我写了一篇“”的文章。这次,使用从Adorner继承来写一个用于图像裁切的选择框。

先看看效果:

裁切选择框

C#代码:

// RubberbandAdorner.cs
#define VISUALCHILD

using System;

using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Documents;

namespace PhotoDemo

{
#if VISUALCHILD
    public class RubberbandAdorner : Adorner
    {
        private Window1 _window;
        public Window1 Window { set { _window = value; } }

        private RectangleGeometry _geometry;

       
        private UIElement _adornedElement;

        private Rect _selectRect;

        public Rect SelectRect { get { return _selectRect; } }

        private System.Windows.Shapes.Path _rubberband;

        public System.Windows.Shapes.Path Rubberband { get { return _rubberband; } }

        protected override int VisualChildrenCount { get { return 1; } }

       
        private Point _anchorPoint;
               
        public RubberbandAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _adornedElement = adornedElement;
            _selectRect = new Rect();
            _geometry = new RectangleGeometry();
            _rubberband = new System.Windows.Shapes.Path();
            _rubberband.Data = _geometry;
            _rubberband.StrokeThickness = 1;
            _rubberband.Stroke = Brushes.Yellow;
            _rubberband.Opacity = .6;
            _rubberband.Visibility = Visibility.Hidden;
            AddVisualChild(_rubberband);

            MouseMove += new MouseEventHandler(DrawSelection);

            MouseUp += new MouseButtonEventHandler(EndSelection);
        }

        protected override Size ArrangeOverride(Size size)

        {
            Size finalSize = base.ArrangeOverride(size);
            ((UIElement)GetVisualChild(0)).Arrange(new Rect(new Point(), finalSize));

            return finalSize;

        }

        public void StartSelection(Point anchorPoint)

        {
            _anchorPoint = anchorPoint;
            _selectRect.Size = new Size(10, 10);
            _selectRect.Location = _anchorPoint;
            _geometry.Rect = _selectRect;
            if (Visibility.Visible != _rubberband.Visibility)
                _rubberband.Visibility = Visibility.Visible;
        }

        private void DrawSelection(object sender, MouseEventArgs e)

        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point mousePosition = e.GetPosition(_adornedElement);

                if (mousePosition.X < _anchorPoint.X)

                    _selectRect.X = mousePosition.X;
                else
                    _selectRect.X = _anchorPoint.X;

                if (mousePosition.Y < _anchorPoint.Y)

                    _selectRect.Y = mousePosition.Y;
                else
                    _selectRect.Y = _anchorPoint.Y;

                _selectRect.Width = Math.Abs(mousePosition.X - _anchorPoint.X);

                _selectRect.Height = Math.Abs(mousePosition.Y - _anchorPoint.Y);

                _geometry.Rect = _selectRect;

                AdornerLayer layer = AdornerLayer.GetAdornerLayer(_adornedElement);
                layer.InvalidateArrange();
            }
        }

        private void EndSelection(object sender, MouseButtonEventArgs e)

        {
            if (3 >= _selectRect.Width || 3 >= _selectRect.Height)
                _rubberband.Visibility = Visibility.Hidden;
            else
                _window.CropButton.IsEnabled = true;

            ReleaseMouseCapture();

        }
   
        protected override Visual GetVisualChild(int index)
        {
            return _rubberband;
        }
    }
#endif

#if NoVISUALCHILD

    public class RubberbandAdorner : Adorner

    {

        private UIElement _adornedElement;

        private bool _showRect;
        private Window1 _window;
        SolidColorBrush _brush;
        Pen _pen;
        private Rect _selectRect;
        public Rect SelectRect { get { return _selectRect; } set { _selectRect = value; } }
        public bool ShowRect { get { return _showRect; } set { _showRect = value; } }
        public Window1 Window { set { _window = value; } }

        public RubberbandAdorner(UIElement adornedElement)

            : base(adornedElement)
        {
            _adornedElement = adornedElement;
            _selectRect = new Rect();
            _brush = new SolidColorBrush();
            _brush.Color = Colors.Yellow;
            _brush.Opacity = .6;
            _pen = new Pen();
            _pen.Thickness = 2;
            _pen.Brush = _brush;
            _showRect = false;
            MouseMove += new MouseEventHandler(DrawSelection);
            MouseUp += new MouseButtonEventHandler(EndSelection);
        }

        public void StartSelection(Point anchorPoint)

        {
            _anchorPoint = anchorPoint;
            _selectRect.Size = new Size(0, 0);
            _selectRect.Location = _anchorPoint;
            if (!_showRect)
                _showRect = true;
        }

        private void DrawSelection(object sender, MouseEventArgs e)

        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point mousePosition = e.GetPosition(_adornedElement);
                if (mousePosition.X < _anchorPoint.X)
                    _selectRect.X = mousePosition.X;
                else
                    _selectRect.X = _anchorPoint.X;
                if (mousePosition.Y < _anchorPoint.Y)
                    _selectRect.Y = mousePosition.Y;
                else
                    _selectRect.Y = _anchorPoint.Y;
                _selectRect.Width = Math.Abs(mousePosition.X - _anchorPoint.X);
                _selectRect.Height = Math.Abs(mousePosition.Y - _anchorPoint.Y);
                InvalidateArrange();
                AdornerLayer layer = AdornerLayer.GetAdornerLayer(_adornedElement);
                layer.InvalidateArrange();
            }
        }

        private void EndSelection(object sender, MouseButtonEventArgs e)

        {
            if (3 >= _selectRect.Width || 3 >= _selectRect.Height)
                ShowRect = false;
            else
                _window.CropButton.IsEnabled = true;
            ReleaseMouseCapture();
        }

        protected override void OnRender(DrawingContext drawingContext)

        {
            if (_showRect)
            {
                base.OnRender(drawingContext);
                drawingContext.DrawRectangle(Brushes.Transparent, _pen, _selectRect);
            }
            else
                return;
        }
    }
#endif

}

目前的代码是画一个实线框,而且是静止的。

有待改进的地方:

1. 如果需要制作类似Photoshop中的“蚂蚁行军”效果呢?如何写程序呢?
2. 如果需要制作类似Photoshop中已框选区域可移动,修改(比如:四周四角加方形手柄)呢? 

转载地址:http://nggix.baihongyu.com/

你可能感兴趣的文章
NTP
查看>>
扩展根目录空间
查看>>
簡單收所有經過網卡的包 promiscuous mode GNU Linux
查看>>
高性能高并发--分布式,集群
查看>>
git获取远程分支
查看>>
文字与格式字符串不匹配
查看>>
我的友情链接
查看>>
window2008 域控更改密码策略
查看>>
redis问题
查看>>
微信服务号接入提示token认证失败
查看>>
CentOS 6.4部署LAMP(多站点环境)
查看>>
javaweb开发之jsp
查看>>
2013年总结(2)-财务收入与支出
查看>>
React创建组件的三种方式
查看>>
Nginx 之 Location基础理解及实战
查看>>
企业级nginx服务优化合集
查看>>
maven配置cargo实现远程部署项目到tomcat
查看>>
ZABBIX API自动检查主机
查看>>
Linux系统基准
查看>>
几个简单的html+css+js题目
查看>>