winform简单截图

//该代码实现简单截图,鼠标左键按下开始截图,移动过程中红线方框确定截图范围,左键放开时弹出保存对话框,选好路径保存就OK。

using System;

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

namespace chartdemo
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        Point startPoint = new Point();
        Point endPoint = new Point();
        int width;
        int height;
        private void Form4_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                startPoint = PointToScreen(e.Location);
            }
        }

        private void Form4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Red, 1);
                this.Refresh();
                Point tP = new Point();
                tP = proccessPoint(startPoint,PointToScreen(e.Location));
                g.DrawRectangle(p, tP.X-this.Location.X-4, tP.Y-this.Location.Y-30, width, height);
            }
        }


        bool flag = false;
        private void Form4_MouseUp(object sender, MouseEventArgs e)
        {
            endPoint = PointToScreen(e.Location);
            startPoint = proccessPoint(startPoint, endPoint);
            if (flag)
            {
                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);//创建显示器的DC
                Graphics g1 = Graphics.FromHdc(dc1);//由句柄创建Graphics
                Bitmap MyImage = new Bitmap(width, height, g1);//创建与屏幕同大小的Bitmap
                Graphics g2 = Graphics.FromImage(MyImage);//获得屏幕的句柄
                IntPtr dc3 = g1.GetHdc();//获得位图的句柄
                IntPtr dc2 = g2.GetHdc();//把当前屏幕捕获到位图中
                BitBlt(dc2, 0, 0, width, height, dc3, startPoint.X, startPoint.Y, 13369376);//把当前屏幕拷贝到位图中
                g1.ReleaseHdc(dc3);//释放屏幕句柄
                g2.ReleaseHdc(dc2);
                string filePath = null;
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.RestoreDirectory = true;//打开上一次所打开的目录
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFileDialog1.FileName.ToString()+".jpeg";
                }
                MyImage.Save(filePath, ImageFormat.Jpeg);
            }
            else
            {
                MessageBox.Show("错误");
            }

        }

        public Point proccessPoint(Point sP,Point eP)
        {
            width = eP.X - sP.X;
            height = eP.Y - sP.Y;
            if (width == 0 || height == 0)
            {
                flag = false;
            }
            else if (width > 0 && height > 0)
            {
                flag = true;
            }
            else if (width > 0 && height < 0)
            {
                sP.Y += height;
                height = Math.Abs(height);
                flag = true;
            }
            else if (width < 0 && height > 0)
            {
                sP.X += width;
                width = Math.Abs(width);
                flag = true;
            }
            else
            {
                sP.X += width;
                sP.Y += height;
                height = Math.Abs(height);
                width = Math.Abs(width);
                flag = true;
            }
            return sP;
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            width = 0;
            height = 0;
        }
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
            IntPtr hdcDest, //  目标设备的句柄   
            int nXDest,    //   目标对象的左上角的X坐标   
            int nYDest,    //   目标对象的左上角的X坐标   
            int nWidth,    //   目标对象的矩形的宽度   
            int nHeight,   //   目标对象的矩形的长度   
            IntPtr hdcSrc, //   源设备的句柄
            int nXSrc,   //   源对象的左上角的X坐标
            int nYSrc,   //   源对象的左上角的X坐标
            System.Int32 dwRop   //   光栅的操作值
            );

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern IntPtr CreateDC(
            string lpszDriver,   //   驱动名称   
            string lpszDevice,   //   设备名称   
            string lpszOutput,   //   无用,可以设定位"NULL"   
            IntPtr lpInitData   //   任意的打印机数据   
            );
    }
}


### 回答1: 在WinForm中进行屏幕截图可以使用C#语言的System.Drawing命名空间下的类库进行操作。主要步骤如下: 首先,需要引入命名空间System.Drawing和System.Windows.Forms。 然后,我们可以使用Screen类的AllScreens属性来获取当前所有屏幕的信息,并确定截图范围。 接下来,可以使用Graphics类创建一个与截图范围相同大小的位图对象,并创建一个用于绘制位图的图形对象。 然后,使用Graphics类的CopyFromScreen方法,将屏幕上指定范围的像素数据复制到位图对象中。 最后,可以使用Bitmap类的Save方法将位图对象保存为指定格式的图片文件。 以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace ScreenShotExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 获取所有屏幕信息 Screen[] screens = Screen.AllScreens; // 创建一个与截图范围相同大小的位图对象 Bitmap bitmap = new Bitmap(screens[0].Bounds.Width, screens[0].Bounds.Height); // 创建一个用于绘制位图的图形对象 Graphics graphics = Graphics.FromImage(bitmap); // 将屏幕上指定范围的像素数据复制到位图对象中 graphics.CopyFromScreen(screens[0].Bounds.X, screens[0].Bounds.Y, 0, 0, screens[0].Bounds.Size); // 保存位图对象为图片文件 bitmap.Save("screenshot.png"); // 释放资源 graphics.Dispose(); bitmap.Dispose(); MessageBox.Show("截图成功并保存为screenshot.png"); } } } ``` 以上代码仅供参考,具体根据实际需求进行修改。 ### 回答2: 在WinForms中实现屏幕截图可以通过使用Screen类和Bitmap类来完成。 首先,我们可以使用Screen类获取当前屏幕的宽度和高度。可以使用Screen类的PrimaryScreen属性来获取主屏幕的信息,也可以使用AllScreens属性获取所有屏幕的信息。 然后,我们可以创建一个Bitmap对象来存储截图图像。可以使用Graphics类和CopyFromScreen方法将屏幕上的图像复制到Bitmap对象中。需要提供复制的起点坐标和大小信息。起点坐标可以设为(0, 0),大小可以设置为屏幕的宽度和高度。 最后,我们可以将Bitmap对象保存为文件。可以使用Save方法来保存图像对象为指定的文件路径。也可以使用其他图像处理方法对截图图像进行进一步处理,如调整大小、添加水印等操作。 以下是一个示例代码,实现了屏幕截图并保存为文件: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace ScreenshotExample { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void btnCapture_Click(object sender, EventArgs e) { // 获取主屏幕信息 Screen screen = Screen.PrimaryScreen; // 创建Bitmap对象 Bitmap screenshot = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // 复制屏幕图像到Bitmap对象中 using (Graphics graphics = Graphics.FromImage(screenshot)) { graphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy); } // 保存Bitmap对象为文件 screenshot.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png); MessageBox.Show("屏幕截图已保存为screenshot.png"); } } } ``` 以上就是使用WinForms实现屏幕截图的基本过程。根据实际需求,可以对截图图像进行进一步处理,如添加边框、调整大小等操作。 ### 回答3: 在WinForms中,我们可以使用System.Drawing命名空间中的类来进行屏幕截图。具体步骤如下: 首先,我们需要创建一个窗体或任何其他WinForms控件作为屏幕截图的载体。在该控件上,我们可以使用Paint事件来绘制屏幕截图。 在Paint事件处理程序中,我们可以使用Graphics类的CopyFromScreen方法来实现屏幕截图的功能。该方法需要指定一个矩形区域,表示要截取的屏幕区域,并将截取的图像绘制到控件上。 具体实现代码如下: private void MyForm_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); // 要截取的屏幕区域,这里以整个窗体区域为例 using (Bitmap bmp = new Bitmap(rect.Width, rect.Height)) // 创建一个与控件大小相同的位图 { using (Graphics g = Graphics.FromImage(bmp)) { // 使用CopyFromScreen方法截取屏幕区域的图像 g.CopyFromScreen(this.PointToScreen(rect.Location), Point.Empty, rect.Size); } // 将截取的图像绘制到控件上 e.Graphics.DrawImage(bmp, Point.Empty); } } 在此代码中,我们先创建一个和控件大小相同的位图bmp,并通过Graphics类的FromImage方法来创建一个Graphics对象g,用于在位图上执行绘制操作。 然后,我们使用CopyFromScreen方法将屏幕区域的图像复制到位图bmp上,其中使用this.PointToScreen方法将控件上的坐标转换为屏幕上的坐标。 最后,将位图bmp绘制到控件上,即可完成屏幕截图的操作。 需要注意的是,由于截取的屏幕图像可能会很大,在操作结束后,记得及时释放位图bmp和Graphics对象g的资源,以避免内存泄漏问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值