利用Adorner制作用于图像裁切的选择框
前天,我写了一篇“使用Adorner显示WPF控件的边界点”的文章。这次,使用从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中已框选区域可移动,修改(比如:四周四角加方形手柄)呢?
最新文章
- Mysql远程连接,并解决wordp主题添加问题
- 【随笔】mOnOwall添加端口映射
- bug_ _java.lang.IllegalArgumentException: View not attached to window manager 2
- 微软云平台媒体服务实践系列 2- 使用动态封装为iOS, Android , Windows 等多平台提供视频点播(VoD)方案
- YII数据库操作(CURD操作)
- U3D 实现地面碰撞效果
- Java单例模式的线程安全问题
- PyQt中登录框设计
- ocp认证考试指南第一章
- Java.nio-随机读写汉字
- MTK Camera相关的Makefile Option详解
- DRF项目创建流程(1)
- 使用 Travis CI 实现项目的持续测试反馈
- [转]天才计算机程序员 -- fabrice bellard
- 如何实现IOS_SearchBar搜索栏及关键字高亮
- 通过micrometer实时监控线程池的各项指标
- 常用的SQL
- BZOJ4245 ONTAK2015 OR-XOR 【位运算+贪心】*
- 简明python教程五----数据结构
- github blog