C# Winform实现截图工具的示例代码

 更新时间:2024年02月15日 08:30:49   作者:搬砖的诗人Z  
这篇文章主要为大家详细介绍了如何使用C# Winform制作一个简单的截图工具,从而实现截图功能,文中的示例代码讲解详细,有需要的可以参考下

效果图

实现代码

截图主要代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OCR
{
    public partial class ScreenBody : Form
    {
        public static Image myImage;


        private Graphics MainPainter;   //主画面
        private Pen pen;                //画笔
        private bool isDowned;          //判断鼠标是否按下 
        private bool RectReady;         //矩形是否绘制完成 
        private Image baseImage;        //基本图形(原来的画面) 
        private Rectangle Rect;         //就是要保存的矩形 
        private Point downPoint;        //鼠标按下的点 
        int tmpx;
        int tmpy;

        public ScreenBody()
        {
            InitializeComponent();
        }

        private void ScreenBody_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Maximized;
                MainPainter = this.CreateGraphics();
                pen = new Pen(Brushes.Blue);
                isDowned = false;
                baseImage = this.BackgroundImage;
                Rect = new Rectangle();
                RectReady = false;
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--ScreenBody_Load");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }

        }

        private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    isDowned = true;

                    if (RectReady == false)
                    {
                        Rect.X = e.X;
                        Rect.Y = e.Y;
                        downPoint = new Point(e.X, e.Y);
                    }
                    if (RectReady == true)
                    {
                        tmpx = e.X;
                        tmpy = e.Y;
                    }
                }
                if (e.Button == MouseButtons.Right)
                {
                    this.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--ScreenBody_MouseDown");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }

        }

        private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDowned = false;
                RectReady = true;
            }
        }

        private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (RectReady == false)
                {
                    if (isDowned == true)
                    {
                        Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y);
                        MainPainter.DrawImage(New, 0, 0);
                        New.Dispose();
                    }
                }
                if (RectReady == true)
                {
                    if (Rect.Contains(e.X, e.Y))
                    {
                        this.Cursor = Cursors.Hand;
                        if (isDowned == true)
                        {
                            //和上一次的位置比较获取偏移量 
                            Rect.X = Rect.X + e.X - tmpx;
                            Rect.Y = Rect.Y + e.Y - tmpy;
                            //记录现在的位置 
                            tmpx = e.X;
                            tmpy = e.Y;
                            MoveRect((Image)baseImage.Clone(), Rect);
                        }
                    }
                    else
                    {
                        this.Cursor = Cursors.Arrow;
                    }
                }
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--ScreenBody_MouseMove");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }

        }


        //画屏幕
        private Image DrawScreen(Image back, int Mouse_x, int Mouse_y)
        {
            try
            {
                Graphics Painter = Graphics.FromImage(back);
                DrawRect(Painter, Mouse_x, Mouse_y);
                return back;
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--DrawScreen");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
                return null;
            }

        }
        //画矩形
        private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
        {
            int width = 0;
            int heigth = 0;
            try
            {
                if (Mouse_y < Rect.Y)
                {
                    Rect.Y = Mouse_y;
                    heigth = downPoint.Y - Mouse_y;
                }
                else
                {
                    heigth = Mouse_y - downPoint.Y;
                }
                if (Mouse_x < Rect.X)
                {
                    Rect.X = Mouse_x;
                    width = downPoint.X - Mouse_x;
                }
                else
                {
                    width = Mouse_x - downPoint.X;
                }
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--DrawRect");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }
            finally
            {
                Rect.Size = new Size(width, heigth);
                Painter.DrawRectangle(pen, Rect);
            }
        }

        //移动矩形
        private void MoveRect(Image image, Rectangle Rect)
        {
            try
            {
                Graphics Painter = Graphics.FromImage(image);
                Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);
                MainPainter.DrawImage(image, 0, 0);
                image.Dispose();
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--MoveRect");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }

        }

        private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                Image memory = new Bitmap(Rect.Width, Rect.Height);
                Graphics g = Graphics.FromImage(memory);
                g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, new Size(Rect.Size.Width - 2, Rect.Size.Height - 2));
                myImage = memory;
                this.Close();
                this.Dispose();
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--ScreenBody_MouseDoubleClick");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }

        }
    }
}

HotKey,热键注册

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace OCR
{
    public class HotKey
    {
        //如果函数执行成功,返回值不为0。
        //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                //要定义热键的窗口的句柄
            int id,                     //定义热键ID(不能与其它ID重复)           
            KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
            Keys vk                     //定义热键的内容
            );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                //要取消热键的窗口的句柄
            int id                      //要取消热键的ID
            );

        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }

    }
}

以上就是C# Winform实现截图工具的示例代码的详细内容,更多关于C# Winform截图的资料请关注脚本之家其它相关文章!

相关文章

  • C#实现在PDF文档中应用多种不同字体

    C#实现在PDF文档中应用多种不同字体

    在PDF文档中,可绘制不同字体样式、不同语言的文字,可通过使用Standard字体、TrueType字体、CJK字体或者自定义(私有)等字体类型。本文将具体介绍实现的方法,需要的可以参考一下
    2022-01-01
  • 使用winapi安装Windows服务示例程序

    使用winapi安装Windows服务示例程序

    这篇文章主要介绍了使用winapi安装Windows服务示例,大家参考使用吧
    2014-01-01
  • C#连接数据库的方法

    C#连接数据库的方法

    ASP.NET连接数据库的技术叫ADO.NET,它是用来向数据库提交sql语句的一堆类。这里连接的是Sql Server 2008数据库,其他数据库用法差不多,就是调用的类名不一样
    2015-11-11
  • C#创建背景色渐变窗体的方法实例

    C#创建背景色渐变窗体的方法实例

    在窗体设计时,可以通过设置窗体的BackColor属性来改变窗口的背景颜色,但是该属性改变后整个窗体的客户区都会变成这种颜色,这样显得非常单调,本文给大家介绍了C#创建背景色渐变窗体的方法实例,需要的朋友可以参考下
    2024-04-04
  • C#将图片存放到SQL SERVER数据库中的方法

    C#将图片存放到SQL SERVER数据库中的方法

    这篇文章主要介绍了C#将图片存放到SQL SERVER数据库中的方法,以实例形式较为详细的分析了C#保存图片到SQL Server数据库的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • 关于finalize机制和引用、引用队列的用法详解

    关于finalize机制和引用、引用队列的用法详解

    下面小编就为大家带来一篇关于finalize机制和引用、引用队列的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • 关于C#线程的全面解析

    关于C#线程的全面解析

    这篇文章主要介绍了关于C#线程的全面解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Unity的AssetPostprocessor之Model函数使用实战

    Unity的AssetPostprocessor之Model函数使用实战

    这篇文章主要为大家介绍了Unity的AssetPostprocessor之Model函数使用实战,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • C#实现清空回收站的方法

    C#实现清空回收站的方法

    这篇文章主要介绍了C#实现清空回收站的方法,涉及C#系统回收站的清空技巧,非常简单实用,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • C# DataTable常见用法汇总

    C# DataTable常见用法汇总

    这篇文章主要介绍了C# DataTable常见用法,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-08-08

最新评论