WPF 图片裁剪框【可拖动】【Winform】

8 篇文章 0 订阅

本案例采用WindowsFormsHost控件作为容器,同时适用于WinForm方向。

要通过WindowsFormsHost使用WinForm中的控件,需要在应用程序中添加对以下程序集的引用:

  • WindowsFormsIntegration
  • System.Windows.Forms

具体资料请查看WPF使用WinForm控件

 

效果图: 

图片裁剪框效果图
图片裁剪框效果图

前端XAML代码:

<Window x:Class="WpfImageResizer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:WpfImageResizer"
        mc:Ignorable="d"
        Title="MainWindow" Height="560" Width="460" WindowStartupLocation="CenterScreen" x:Name="main">
    <Grid >
        <WindowsFormsHost Height="300" Width="300">
            <wf:PictureBox x:Name="_photo"  MouseDown="_photo_MouseDown" MouseMove="_photo_MouseMove" MouseUp="_photo_MouseUp"/>
        </WindowsFormsHost>
        <StackPanel Orientation="Vertical" Grid.Row="1" Margin="0,20">
            <StackPanel  Orientation="Horizontal">
                <TextBlock Text="起点X坐标:" Margin="3"/>
                <TextBlock x:Name="_startX" Text="" Margin="3"/>
                <TextBlock Text="起点Y坐标:" Margin="3"/>
                <TextBlock x:Name="_startY" Text="" Margin="3"/>
            </StackPanel>
            <StackPanel  Orientation="Horizontal">
                <TextBlock Text="终点X坐标:" Margin="3"/>
                <TextBlock x:Name="_endX" Text="" Margin="3"/>
                <TextBlock Text="终点Y坐标:" Margin="3"/>
                <TextBlock x:Name="_endY" Text="" Margin="3"/>
            </StackPanel>
            <StackPanel  Orientation="Horizontal">
                <TextBlock Text="裁剪框长度:" Margin="3"/>
                <TextBlock x:Name="_sizeW" Text="" Margin="3"/>
                <TextBlock Text="裁剪框高度:" Margin="3"/>
                <TextBlock x:Name="_sizeH" Text="" Margin="3"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

这里需要你添加引用,不然是没办法打出WindowsFormsHost控件

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

同时也需要你在程序集添加【System.Windows.Forms】引用。

StackPanel块只是为了输出裁剪框的具体信息,实际情况可以删除。

如果你是WinForm端,XAML块请忽视,只需要在WinForm创建一个PictureBox控件即可。


图片裁剪框辅助类:

public class ResizerHelper
    {
        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="maxwidth">裁剪框最大宽度</param>
        /// <param name="maxheight">裁剪框最大高度</param>
        /// <param name="size">裁剪框边框宽度</param>
        /// <param name="pic">绘制容器</param>
        public ResizerHelper(int maxwidth, int maxheight, int size, System.Windows.Forms.Control pic)
        {
            MaxWidth = maxwidth;
            MaxHeight = maxheight;
            PenSize = size;
            ControlPic = pic;
            IsDrawing = true;
        }

        /// <summary>
        /// 裁剪框最大宽度
        /// </summary>
        private static int MaxWidth { get; set; }

        /// <summary>
        /// 裁剪框最大高度
        /// </summary>
        private static int MaxHeight { get; set; }

        private static System.Drawing.Point startPoint = System.Drawing.Point.Empty;
        /// <summary>
        /// 矩形的相对起点(始终在矩形的左上角)
        /// </summary>
        public static System.Drawing.Point StartPoint
        {
            get { return startPoint; }
            set
            {
                if (value.X > -1 && value.X < (MaxWidth + 1))
                    startPoint.X = value.X;
                if (value.Y > -1 && value.Y < (MaxHeight + 1))
                    startPoint.Y = value.Y;
            }
        }

        private static System.Drawing.Point endPoint = System.Drawing.Point.Empty;
        /// <summary>
        /// 矩形的相对终点(始终在矩形的右下角)
        /// </summary>
        public static System.Drawing.Point EndPoint
        {
            get { return endPoint; }
            set
            {
                if (value.X > -1 && value.X < (MaxWidth + 1))
                    endPoint.X = value.X;
                if (value.Y > -1 && value.Y < (MaxHeight + 1))
                    endPoint.Y = value.Y;
            }
        }

        /// <summary>
        /// 点击矩形时起点所在位置(固定不变)
        /// </summary>
        private static System.Drawing.Point FixedStartPoint { get; set; }

        /// <summary>
        /// 点击矩形时终点所在位置(固定不变)
        /// </summary>
        private static System.Drawing.Point FixedEndPoint { get; set; }

        private static Dictionary<int, int> _distance;
        /// <summary>
        /// 鼠标在裁剪区第一次点击时的坐标与固定起点的距离
        /// </summary>
        private static Dictionary<int, int> Distance
        {
            get {
                if (_distance==null)
                {
                    _distance = new Dictionary<int, int>();
                }
                return _distance;
            }
            set {
                
                _distance = value;
            }
        }

        /// <summary>
        /// 绘制容器
        /// </summary>
        private static System.Windows.Forms.Control ControlPic { get; set; }

        /// <summary>
        /// 画笔大小
        /// </summary>
        private static int PenSize { get; set; }

        /// <summary>
        /// 鼠标在容器时的样式
        /// </summary>
        private static CursorStyleEnum CursorStyle { get; set; }
        
        /// <summary>
        /// 是否在绘制矩形
        /// </summary>
        private static bool IsDrawing { get; set; }

        /// <summary>
        /// 按下鼠标
        /// </summary>
        /// <param name="point">鼠标点击坐标点</param>
        public static void MouseDown(System.Drawing.Point point)
        {
            if (ControlPic == null)
                return;
            if (IsDrawing)
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.Cross;
                StartPoint = point;
                //第一次点击不确定,会导致无法360°画矩形
                FixedStartPoint = point;
            }
            else
            {
                SetFixedPoint();
                if (CursorStyle != CursorStyleEnum.Middle)
                {
                    SetPointByCursor(point);
                    DrawRectangle();
                }
                else
                {
                    int disX = point.X - FixedStartPoint.X;
                    int disY = point.Y - FixedStartPoint.Y;
                    Distance.Add(disX, disY);
                }
            }
        }

        /// <summary>
        /// 移动鼠标
        /// </summary>
        /// <param name="point">鼠标点击坐标点</param>
        public static void MouseMove(System.Drawing.Point point)
        {
            if (ControlPic == null)
                return;
            if (IsDrawing && (System.Windows.Forms.Control.MouseButtons == System.Windows.Forms.MouseButtons.Left))//鼠标需要按住左键,并移动才能绘制裁剪框.
            {
                //第一次点击类始于拖动右下角移动
                PointMoveBySW(point);
                DrawRectangle();
            }
            else if (!IsDrawing && (System.Windows.Forms.Control.MouseButtons == System.Windows.Forms.MouseButtons.Left))//修改裁剪框大小
            {
                SetPointByCursor(point);
                DrawRectangle();
            }
            else//单纯的移动
                SetCursorStyle(point);
        }

        /// <summary>
        /// 释放鼠标
        /// </summary>
        /// <param name="point">鼠标点击坐标点</param>
        public static void MouseUp(System.Drawing.Point point)
        {
            if (ControlPic == null)
                return;
            if (EndPoint.IsEmpty)
            {
                StartPoint = System.Drawing.Point.Empty;
                ControlPic.Cursor = System.Windows.Forms.Cursors.Arrow;
                return;
            }
            if (IsDrawing)
            {
                IsDrawing = false;//绘制矩形结束
                FixedStartPoint= System.Drawing.Point.Empty;
            }
            else
            {
                SetFixedPoint(true);
                Distance.Clear();
            }
            GC.Collect();
        }

        /// <summary>
        /// 获取裁剪框的宽度
        /// </summary>
        public static int GetRectangleWidth()
        {
            if (!IsDrawing && CursorStyle == CursorStyleEnum.Middle && !FixedStartPoint.IsEmpty && !FixedEndPoint.IsEmpty)
                return Math.Abs(FixedEndPoint.X - FixedStartPoint.X);
            else
                return Math.Abs(EndPoint.X - StartPoint.X);
        }

        /// <summary>
        /// 获取裁剪框的高度
        /// </summary>
        public static int GetRectangleHeight()
        {
            if (!IsDrawing && CursorStyle == CursorStyleEnum.Middle && !FixedStartPoint.IsEmpty && !FixedEndPoint.IsEmpty)
                return Math.Abs(FixedEndPoint.Y - FixedStartPoint.Y);
            else
                return Math.Abs(EndPoint.Y - StartPoint.Y);
        }

        /// <summary>
        /// 根据鼠标点击的位置重新设置裁剪框起点和终点坐标
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void SetPointByCursor(System.Drawing.Point point)
        {
            switch (CursorStyle)
            {
                case CursorStyleEnum.Up:
                    PointMoveByUp(point);
                    break;
                case CursorStyleEnum.Down:
                    PointMoveByDown(point);
                    break;
                case CursorStyleEnum.Left:
                    PointMoveByLeft(point);
                    break;
                case CursorStyleEnum.Right:
                    PointMoveByRight(point);
                    break;
                case CursorStyleEnum.NE:
                    PointMoveByNE(point);
                    break;
                case CursorStyleEnum.NW:
                    PointMoveByNW(point);
                    break;
                case CursorStyleEnum.SW:
                    PointMoveBySW(point);
                    break;
                case CursorStyleEnum.SE:
                    PointMoveBySE(point);
                    break;
                case CursorStyleEnum.Middle:
                default:
                    PointMoveByMiddle(point);
                    break;
            }
        }

        /// <summary>
        /// 移动上横线
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByUp(System.Drawing.Point point)
        {
            if (point.Y > FixedEndPoint.Y)
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, FixedEndPoint.Y);
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
            }
            else
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
            }
        }

        /// <summary>
        /// 移动下横线
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByDown(System.Drawing.Point point)
        {
            if (point.Y < FixedStartPoint.Y)
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, FixedStartPoint.Y);
            }
            else
            {
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
            }
        }

        /// <summary>
        /// 移动左横线
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByLeft(System.Drawing.Point point)
        {
            if (point.X > FixedEndPoint.X)
            {
                StartPoint = new System.Drawing.Point(FixedEndPoint.X, FixedStartPoint.Y); ;
                EndPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
            }
            else
            {
                StartPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
            }
        }

        /// <summary>
        /// 移动右横线
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByRight(System.Drawing.Point point)
        {
            if (point.X < FixedStartPoint.X)
            {
                StartPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
                EndPoint = new System.Drawing.Point(FixedStartPoint.X, FixedEndPoint.Y);
            }
            else
            {
                EndPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
            }
        }

        /// <summary>
        /// 移动左上角
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByNE(System.Drawing.Point point)
        {
            if (point.X < FixedEndPoint.X && point.Y > FixedEndPoint.Y)//下方
            {
                StartPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
            }
            else if (point.X > FixedEndPoint.X && point.Y < FixedEndPoint.Y)//右方
            {
                StartPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
                EndPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
            }
            else if (point.X > FixedEndPoint.X && point.Y > FixedEndPoint.Y)//右下方
            {
                StartPoint = FixedEndPoint;
                EndPoint = point;
            }
            else
            {
                StartPoint = point;
            }
        }

        /// <summary>
        /// 移动右上角
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByNW(System.Drawing.Point point)
        {
            if (point.X > FixedStartPoint.X && point.Y > FixedEndPoint.Y)//下方
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, FixedEndPoint.Y);
                EndPoint = point;
            }
            else if (point.X < FixedStartPoint.X && point.Y < FixedEndPoint.Y)//左方
            {
                StartPoint = point;
                EndPoint = new System.Drawing.Point(FixedStartPoint.X, FixedEndPoint.Y); ;
            }
            else if (point.X < FixedStartPoint.X && point.Y > FixedEndPoint.Y)//左下方
            {
                StartPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
                EndPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
            }
            else
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
                EndPoint = new System.Drawing.Point(point.X, FixedEndPoint.Y);
            }
        }

        /// <summary>
        /// 移动右下角
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveBySW(System.Drawing.Point point)
        {
            if (point.X > FixedStartPoint.X && point.Y < FixedStartPoint.Y)//上方
            {
                StartPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
                EndPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
            }
            else if (point.X < FixedStartPoint.X && point.Y > FixedStartPoint.Y)//左方
            {
                StartPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
                EndPoint = new System.Drawing.Point(FixedStartPoint.X, point.Y);
            }
            else if (point.X < FixedStartPoint.X && point.Y < FixedStartPoint.Y)//左上方
            {
                StartPoint = point;
                EndPoint = FixedStartPoint;
            }
            else
            {
                EndPoint = point;
            }
        }

        /// <summary>
        /// 移动左下角
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveBySE(System.Drawing.Point point)
        {
            if (point.X < FixedEndPoint.X && point.Y < FixedStartPoint.Y)//上方
            {
                StartPoint = point;
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, FixedStartPoint.Y);
            }
            else if (point.X > FixedEndPoint.X && point.Y > FixedStartPoint.Y)//右方
            {
                StartPoint = new System.Drawing.Point(FixedEndPoint.X, FixedStartPoint.Y);
                EndPoint = point;
            }
            else if (point.X > FixedEndPoint.X && point.Y < FixedStartPoint.Y)//右上方
            {
                StartPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
                EndPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
            }
            else
            {
                StartPoint = new System.Drawing.Point(point.X, FixedStartPoint.Y);
                EndPoint = new System.Drawing.Point(FixedEndPoint.X, point.Y);
            }
        }

        /// <summary>
        /// 整体移动
        /// </summary>
        /// <param name="point">鼠标移动的位置</param>
        private static void PointMoveByMiddle(System.Drawing.Point point)
        {
            //计算将要移动后的起点坐标
            int dicKey = Distance.FirstOrDefault().Key;
            int newSX = point.X - dicKey;
            int newSY = point.Y - Distance[dicKey];
            if (newSX + GetRectangleWidth() > MaxWidth)
                newSX = StartPoint.X;
            if (newSY + GetRectangleHeight() > MaxHeight)
                newSY = StartPoint.Y;
            StartPoint = new System.Drawing.Point(newSX, newSY);
            EndPoint = new System.Drawing.Point((newSX + GetRectangleWidth()), (newSY + GetRectangleHeight()));
        }

        /// <summary>
        /// 设置固定的矩形起点和终点坐标
        /// </summary>
        /// <param name="isEmpty">是否恢复默认值</param>
        private static void SetFixedPoint(bool isEmpty = false)
        {
            if (isEmpty)
            {
                FixedStartPoint = System.Drawing.Point.Empty;
                FixedEndPoint = System.Drawing.Point.Empty;
            }
            else
            {
                FixedStartPoint = new System.Drawing.Point(Math.Min(StartPoint.X, EndPoint.X), Math.Min(StartPoint.Y, EndPoint.Y));
                FixedEndPoint = new System.Drawing.Point(Math.Max(StartPoint.X, EndPoint.X), Math.Max(StartPoint.Y, EndPoint.Y));
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        private static void DrawRectangle()
        {
            if (EndPoint.IsEmpty && IsDrawing)
                return;
            using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red))
            {
                pen.Width = PenSize;
                using (System.Drawing.Graphics g = ControlPic.CreateGraphics())
                {
                    DrawRefresh();
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//设置抗锯齿
                    g.DrawRectangle(pen, StartPoint.X, StartPoint.Y, GetRectangleWidth(), GetRectangleHeight());
                }
            }
        }

        /// <summary>
        /// 消除以前的绘制痕迹
        /// </summary>
        private static void DrawRefresh()
        {
            ControlPic.Refresh();
        }

        /// <summary>
        /// 设置鼠标在裁剪区的样式
        /// </summary>
        private static void SetCursorStyle(System.Drawing.Point point)
        {
            if (IsDrawing)//只有绘制完裁剪框,才根据鼠标位置更改鼠标样式
                return;
            if (StartPoint.IsEmpty && EndPoint.IsEmpty)
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.Arrow;
                CursorStyle = CursorStyleEnum.Arrow;
                return;
            }
            if (point.X > StartPoint.X && point.X < EndPoint.X && point.Y < StartPoint.Y)//上横线
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNS;
                CursorStyle = CursorStyleEnum.Up;
            }
            else if (point.X > StartPoint.X && point.X < EndPoint.X && point.Y > EndPoint.Y)//下横线
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNS;
                CursorStyle = CursorStyleEnum.Down;
            }
            else if (point.Y > StartPoint.Y && point.Y < EndPoint.Y && point.X < StartPoint.X)//左横线
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeWE;
                CursorStyle = CursorStyleEnum.Left;
            }
            else if (point.Y > StartPoint.Y && point.Y < EndPoint.Y && point.X > EndPoint.X)//右横线
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeWE;
                CursorStyle = CursorStyleEnum.Right;
            }
            else if (point.X <= StartPoint.X && point.Y<= StartPoint.Y)//左上角
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
                CursorStyle = CursorStyleEnum.NE;
            }
            else if (point.X >= EndPoint.X && point.Y <= StartPoint.Y)//右上角
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNESW;
                CursorStyle = CursorStyleEnum.NW;
            }
            else if (point.X >= EndPoint.X && point.Y >= EndPoint.Y)//右下角
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
                CursorStyle = CursorStyleEnum.SW;
            }
            else if (point.X <= StartPoint.X && point.Y >= EndPoint.Y)//在左下角
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeNESW;
                CursorStyle = CursorStyleEnum.SE;
            }
            else
            {
                ControlPic.Cursor = System.Windows.Forms.Cursors.SizeAll;
                CursorStyle = CursorStyleEnum.Middle;
            }
        }

        /// <summary>
        /// 重置状态
        /// </summary>
        public static void Reset()
        {
            DrawRefresh();
            IsDrawing = true;
            SetFixedPoint(true);
            Distance.Clear();
            StartPoint = System.Drawing.Point.Empty;
            EndPoint= System.Drawing.Point.Empty;
            if (ControlPic != null)
                ControlPic.Cursor = System.Windows.Forms.Cursors.Arrow;
            CursorStyle = CursorStyleEnum.Arrow;
            GC.Collect();
        }
    }

EnumCursorPosition枚举:控制鼠标所处位置时显示的样式,例如在裁剪框的左下角,可以给用户显示向左下拉扯的样式。

里面的某些属性你可能会发现并没有在辅助类中使用,请忽略!!

每段内容我都写了注释,如果你没看懂,请在评论区留言。


        IsDrawing是用来判断是否已经绘制出矩形的属性,用于区分是在绘制矩形还是在修改矩形大小。
        FixedStartPointFixedEndPoint两个坐标点是在点击的时候获取鼠标的位置,通过鼠标移动改变矩形的时候,需要用着两个点找到一个参考点,无论你360°怎么拖动,那个点可以视为固定点。
       矩形整体移动的时候,Distance是用来保存鼠标点击的坐标点与矩形左上角【起点】的矩形,Key为X差,Value为Y差。


       反映鼠标当前所处位置的枚举

    /// <summary>
    /// 鼠标所处位置
    /// </summary>
    public enum CursorStyleEnum
    {
        /// <summary>
        /// 在裁剪区
        /// </summary>
        Middle = 1,
        /// <summary>
        /// 左竖线
        /// </summary>
        Left = 2,
        /// <summary>
        /// 上横线
        /// </summary>
        Up = 3,
        /// <summary>
        /// 右竖线
        /// </summary>
        Right = 4,
        /// <summary>
        /// 下横线
        /// </summary>
        Down = 5,
        /// <summary>
        /// 左上角
        /// </summary>
        NE = 6,
        /// <summary>
        /// 右上角
        /// </summary>
        NW = 7,
        /// <summary>
        /// 右下角
        /// </summary>
        SW = 8,
        /// <summary>
        /// 左下角
        /// </summary>
        SE = 9,
        /// <summary>
        /// 光标
        /// </summary>
        Arrow = 10
    }


       如果你们有兴趣的话,可以用设计模式重新设计一遍ResizerHelper。

 


CS后端代码:

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //假设已经拍摄好了照片,并且按下了【裁剪】按钮
            _photo.BackgroundImage = System.Drawing.Bitmap.FromFile("../../Images/1.jpg");
            _photo.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            //初始化辅助类
            ResizerHelper helpers = new ResizerHelper(300, 300, 3, this._photo);
            //动态创建右键菜单
            CreateContextMenu(this._photo);
        }
        /// <summary>
        /// 按下鼠标左键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _photo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//避免右键触发
            {
                ResizerHelper.MouseDown(e.Location);
            }
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _photo_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ResizerHelper.MouseMove(e.Location);
            Print();
        }
        /// <summary>
        /// 松开鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _photo_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//避免右键触发
            {
                ResizerHelper.MouseUp(e.Location);
            }
        }
        /// <summary>
        /// 输出矩形裁剪框信息
        /// </summary>
        private void Print()
        {
            this._startX.Text = ResizerHelper.StartPoint.X.ToString();
            this._startY.Text = ResizerHelper.StartPoint.Y.ToString();
            this._endX.Text = ResizerHelper.EndPoint.X.ToString();
            this._endY.Text = ResizerHelper.EndPoint.Y.ToString();
            this._sizeW.Text = ResizerHelper.GetRectangleWidth().ToString();
            this._sizeH.Text = ResizerHelper.GetRectangleHeight().ToString();
        }

        /// <summary>
        /// 创建右键菜单
        /// </summary>
        /// <param name="control">绑定控件</param>
        private void CreateContextMenu(System.Windows.Forms.Control control)
        {
            List<System.Windows.Forms.MenuItem> menuList = new List<System.Windows.Forms.MenuItem>();
            System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem();
            menu.Text = "重置";
            menu.Click += Menu_Click;
            menuList.Add(menu);
            System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu(menuList.ToArray());
            control.ContextMenu = contextMenu;
        }

        /// <summary>
        /// 右键菜单【重置】功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Menu_Click(object sender, EventArgs e)
        {
            ResizerHelper.Reset();
        }
        
    }

cs端的代码没什么具体内容了,都是些调用辅助类的方法,同时限制鼠标的左右键。

【重新绘制】功能原本是通过双击右键的方式调用,怕出现误操作功亏一篑,现在改成了点击右键菜单中的【重置】按钮来实现。


项目源码:

百度网盘链接:https://pan.baidu.com/s/1Ckb8Db9F_DncZcOHwDvGYQ  提取码:7c42
CSDN下载:https://download.csdn.net/download/Dear200892/12518102

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值