http://s10/mw690/002YNZ93zy747X9Zi0F79&690
转自:http://bbs.csdn.net/topics/390922046
中crystal_lz回答的代码 本人还未验证代码 先贴出来
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace XXXXXXXXXXXXXXXXX
{
public class
ImageCroppingBox : Control
{
public ImageCroppingBox() {
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor,
true);
this.Size = new Size(200, 150);
}
private Image _Image;
///
/// 要裁剪的图像
///
[Description("要裁剪的图像"), Category("Customs")]
public Image Image {
get { return _Image; }
set {
if (value == this._Image) return;
_Image = value;
this.Clear();
}
}
private Color _MaskColor = Color.FromArgb(125, 0, 0, 0);
///
/// 遮罩颜色
///
[Description("遮罩颜色"), Category("Customs")]
public Color MaskColor {
get { return _MaskColor; }
set {
if (_MaskColor == value) return;
_MaskColor = value;
if (this._Image != null) this.Invalidate();
}
}
private bool _IsLockSelected;
///
/// 是否锁定当前选择 锁定后无法重新拖选区域
///
[Description("是否锁定当前选择 锁定后无法重新拖选区域"), Category("Customs")]
public bool IsLockSelected {
get { return _IsLockSelected; }
set { _IsLockSelected = value; }
}
private bool _IsSetClip;
///
/// 是否限定在拖动区域时 限定鼠标范围
///
[Description("是否限定在拖动区域时 限定鼠标范围"), Category("Customs")]
public bool IsSetClip {
get { return _IsSetClip; }
set { _IsSetClip = value; }
}
private bool _IsDrawMagnifier;
///
/// 是否显示放大镜
///
[Description("是否显示放大镜"), Category("Customs")]
public bool IsDrawMagnifier {
get { return _IsDrawMagnifier; }
set { _IsDrawMagnifier = value; }
}
private bool _IsDrawed;
///
/// 获取当前是否已经选择区域
///
[Description("获取当前是否已经选择区域"), Category("Customs")]
public bool IsDrawed {
get { return _IsDrawed; }
}
private Rectangle _SelectedRectangle;
///
/// 获取或设置悬着区域
///
[Description("获取或设置选择的区域"), Category("Customs")]
public Rectangle SelectedRectangle {
get { return _SelectedRectangle; }
set {
if (this._IsSetClip)
value.Intersect(this.DisplayRectangle);
if (this._SelectedRectangle == value) return;
this._SelectedRectangle = value;
m_ptTempForMove = value.Location;
this.Invalidate();
this._IsDrawed = true;
}
}
private Point
m_ptStart;
//起始点位置
private Point
m_ptCurrent;
//当前鼠标位置
private Point m_ptTempForMove; //移动选框的时候临时用
private Rectangle m_rectClip;
//限定鼠标活动的区域
private Rectangle[] m_rectDots = new
Rectangle[8]; //八个控制点
protected bool m_bMoving;
protected bool m_bChangeWidth;
protected bool m_bChangeHeight;
protected bool m_bMouseHover;
//初始化控件
protected override void OnCreateControl() {
for (int i = 0; i < 8; i++) {
m_rectDots[i].Size = new Size(5, 5);
}
this.BackColor = Color.Black;
this._IsSetClip = true;
//this._IsDrawMagnifier = true;
base.OnCreateControl();
this.MouseEnter += (s, e) => m_bMouseHover = true;
this.MouseLeave += (s, e) => {
m_bMouseHover = false;
if (this._IsDrawMagnifier) this.Invalidate();
};
}
protected override void OnMouseDown(MouseEventArgs e) {
if (this._Image == null || this._IsLockSelected) {
base.OnMouseDown(e);
return;//Image属性null或者已经锁定选择 直接返回
}
//如果限定了鼠标范围 判断若不在限定范围内操作 返回
if (this._IsSetClip) {
if (e.X > this._Image.Width || e.Y > this._Image.Height)
{
base.OnMouseDown(e);
return;
}//否则计算需要限定鼠标的区域
m_rectClip = this.DisplayRectangle;
Size sz = this.Size;
if (this._Image != null) sz = this._Image.Size;
m_rectClip.Intersect(new Rectangle(Point.Empty, sz));
m_rectClip.Width++; m_rectClip.Height++;
Cursor.Clip = RectangleToScreen(m_rectClip);