C#实现鼠标拖拽区域截图

这是一个C#实现的图形用户界面截图工具,具备选择区域截图、编辑(矩形、椭圆、箭头、线条、文本)和撤销功能。工具使用了枚举类型定义操作类型和绘制样式,以及自定义类来存储操作数据,并通过IDisposable接口进行资源管理。
摘要由CSDN通过智能技术生成

代码

  internal enum SizeGrip
    {
   
        None = 0,
        Top,
        Bottom,
        Left,
        Right,
        TopLeft,
        TopRight,
        BottomLeft,
        BottomRight,
        All
    }

    internal class OperateObject
    {
   
        private OperateType _operateType;
        private Color _color;
        private object _data;

        public OperateObject() {
    }

        public OperateObject(
            OperateType operateType, Color color, object data)
        {
   
            _operateType = operateType;
            _color = color;
            _data = data;
        }

        public OperateType OperateType
        {
   
            get {
    return _operateType; }
            set {
    _operateType = value; }
        }

        public Color Color
        {
   
            get {
    return _color; }
            set {
    _color = value; }
        }

        public object Data
        {
   
            get {
    return _data; }
            set {
    _data = value; }
        }
    }

    internal enum OperateType
    {
   
        None = 0,
        DrawRectangle,
        DrawEllipse,
        DrawArrow,
        DrawLine,
        DrawText
    }

    internal class OperateManager : IDisposable
    {
   
        private List<OperateObject> _operateList;

        private static readonly int MaxOperateCount = 1000;

        public OperateManager()
        {
   
        }

        public List<OperateObject> OperateList
        {
   
            get
            {
   
                if (_operateList == null)
                {
   
                    _operateList = new List<OperateObject>(100);
                }
                return _operateList;
            }
        }

        public int OperateCount
        {
   
            get {
    return OperateList.Count; }
        }

        public void AddOperate(
            OperateType operateType,
            Color color,
            object data)
        {
   
            OperateObject obj = new OperateObject(
                operateType, color, data);
            if (OperateList.Count > MaxOperateCount)
            {
   
                OperateList.RemoveAt(0);
            }
            OperateList.Add(obj);
        }

        public bool RedoOperate()
        {
   
            if (OperateList.Count > 0)
            {
   
                OperateList.RemoveAt(OperateList.Count - 1);
                return true;
            }
            return false;
        }

        public void Clear()
        {
   
            OperateList.Clear();
        }

        #region IDisposable 成员

        public void Dispose()
        {
   
            if (_operateList != null)
            {
   
                _operateList.Clear();
                _operateList = null;
            }
        }

        #endregion
    }

    public enum DrawStyle
    {
   
        None = 0,
        Rectangle,
        Ellipse,
        Arrow,
        Text,
        Line
    }

    internal class DrawTextData
    {
   
        private string _text;
        private Font _font;
        private Rectangle _textRect;
        private bool _completed;

        public DrawTextData() {
    }

        public DrawTextData(string text, Font font, Rectangle textRect)
        {
   
            _text = text;
            _font = font;
            _textRect = textRect;
        }

        public string Text
        {
   
            get {
    return _text; }
            set {
    _text = value; }
        }

        public Font Font
        {
   
            get {
    return _font; }
            set {
    _font = value; }
        }

        public Rectangle TextRect
        {
   
            get {
    return _textRect; }
            set {
    _textRect = value; }
        }

        public bool Completed
        {
   
            get {
    return _completed; }
            set {
    _completed = value; }
        }
    }

    internal class NativeMethods
    {
   
        public const int WS_EX_TRANSPARENT = 0x00000020;

        [DllImport("user32.dll")]
        public static extern bool ClipCursor(ref RECT lpRect);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll")]
        public static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(
            IntPtr hObject, int nXDest, int nYDest, int nWidth,
           int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc,
            TernaryRasterOperations dwRop);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr LoadLibrary(string lpFileName);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
   
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;

            public RECT(int left, int top, int right, int bottom)
            {
   
                Left = left;
                Top = top;
                Right = right;
                Bottom = bottom;
            }

            public RECT(Rectangle rect)
            {
   
                Left = rect.Left;
                Top = rect.Top;
                Right = rect.Right;
                Bottom = rect.Bottom;
            }

            public Rectangle Rect
            {
   
                get
                {
   
                    return new Rectangle(
                        Left,
                        Top,
                        Right - Left,
                        Bottom - Top);
                }
            }

            public Size Size
            {
   
                get
                {
   
                    return new Size(Right - Left, Bottom - Top);
                }
            }

            public static RECT FromXYWH(int x, int y, int width, int height)
            {
   
                return new RECT(x,
                                y,
                                x + width,
                                y + height);
            }

            public static RECT FromRectangle(Rectangle rect)
            {
   
                return new RECT(rect.Left,
                                 rect.Top,
                                 rect.Right,
                                 rect.Bottom);
            }
        }

        public enum TernaryRasterOperations
        {
   
            SRCCOPY = 0x00CC0020, /* dest = source*/
            SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
            SRCAND = 0x008800C6, /* dest = source AND dest*/
            SRCINVERT = 0x00660046, /* dest = source XOR dest*/
            SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
            NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
            NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
            MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
            MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
            PATCOPY = 0x00F00021, /* dest = pattern*/
            PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
            PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
            DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
            BLACKNESS = 0x00000042, /* dest = BLACK*/
            WHITENESS = 0x00FF0062, /* dest = WHITE*/
        }
    }

    /// <summary>
    /// 鼠标拖拽区域截图
    /// </summary>
    public class CaptureImageTool : Form
    {
   
        /// <summary>
        /// 遮罩层颜色
        /// </summary>
        private static SolidBrush mask = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
        /// <summary>
        /// 原始屏幕图
        /// </summary>
        private Image ScreenImage;

        #region Fields

        private Image _image;
        private CaptureImageToolColorTable _colorTable;
        private Cursor _selectCursor = Cursors.Default;
        private Cursor _drawCursor = Cursors.Cross;

        private Point _mouseDownPoint;
        private Point _endPoint;
        private bool _mouseDown;
        private Rectangle _selectImageRect;
        private Rectangle _selectImageBounds;
        private bool _selectedImage;
        private SizeGrip _sizeGrip;
        private Dictionary<SizeGrip, Rectangle> _sizeGripRectList;
        private OperateManager _operateManager;
        private List<Point> _linePointList;

        private static readonly Font TextFont =
           new Font("Times New Roman", 12F, FontStyle.Bold, GraphicsUnit.Point, 0);
        //private static readonly string ToolTipStartCapture = "按住左键不放选择截图区域";

        #endregion

        #region Constructors

        public CaptureImageTool()
        {
   
            Init();
            KeyUp += CaptureImageTool_KeyUp;
        }

        #endregion

        #region Properties

        [Browsable(false)]
        [
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值