Winform截图

本文属C#系列文章目录

使用WinForm演示,重点在于方法。

最近新文章:C#Winform批量获取CSV内容展示,并保存



2023/3/10更新内容:

今天看见一个WinForm只截取该控件区域的方法:

此实例中以panel1控件的长宽截图保存。

Bitmap map = new Bitmap(panel1.Width, panel1.Height);
panel3.DrawToBitmap(map, panel1.ClientRectangle);
map.Save("test.jpg");

 分享给大家。


正文:

一、最大化最小化按钮

 设置4个button控件

添加最大化最小化按钮事件

代码如下

        private void btn_max_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

二、全屏截取

 全屏截图方法如下:

        //用于双击截图
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(IntPtr hdcDest, // 目标 DC的句柄
                                          int nXDest,
                                          int nYDest,
                                          int nWidth,
                                          int nHeight,
                                          IntPtr hdcSrc,  // 源DC的句柄
                                          int nXSrc,
                                          int nYSrc,
                                          System.Int32 dwRop  // 光栅的处理数值
                                          );

        //全屏
        public void saveA()
        {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);//获取当前屏幕大小

            Graphics g1 = this.CreateGraphics();//创建一个以当前屏幕为模板的对象
            Image myImage = new Bitmap(rect.Width, rect.Height, g1);//创建以屏幕大小为标准的位图

            Graphics g2 = Graphics.FromImage(myImage);
            IntPtr dc1 = g1.GetHdc();//得到屏幕的dc
            IntPtr dc2 = g2.GetHdc();//得到bitmap的dc
            BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);//实现屏幕捕获
            g1.ReleaseHdc(dc1);//释放屏幕的dc
            g2.ReleaseHdc(dc2);//释放bitmap的dc

            string fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取应用程序运行的路径

            //可以根据需要,把屏幕以其他图片的格式来保存,如想把图片保存为位图文件,可以把"ImageFormat.Jpeg"改换成"ImageFormat.Bmp";
            //想把图片保存为Gif文件,就把"ImageFormat.Jpeg"改换成"ImageFormat.Gif"。可以保存的文件类型大概有十多种
            myImage.Save(filePath + fileName, ImageFormat.Jpeg);
            MessageBox.Show("文件保存在:" + filePath + fileName);
        }

 使用button1控件的点击事件调用全屏截图的方法:

private void button1_Click(object sender, EventArgs e)
        {
            saveA();
        }

 运行程序保存结果:

 黑色块是因为截取的是窗体的全屏,把窗体放大效果如下:

 还是有黑边,正常,是因为窗体默认有边框样式,设置样式为None即可

在Form1窗体中添加边框样式为None

public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
        }

再次放大窗体:

 成功。


三、指定区域截图

  区域截图方法如下:

//指定区域
        private void saveB()
        {
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取应用程序运行的路径
            string fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名
            try
            {
                Screen screen = Screen.AllScreens.FirstOrDefault();//获取当前第一块屏幕(根据需求也可以换其他屏幕)
                //创建需要截取的屏幕区域  
                Rectangle rc = new Rectangle(0, 0, 200, 200);
                //生成截图的位图容器  
                Bitmap bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
                //GDI+图像画布  
                using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
                {
                    memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);//对屏幕指定区域进行图像复制
                }
                bitmap.Save(filePath + fileName, ImageFormat.Png);
                MessageBox.Show("文件保存在:" + filePath + fileName);
            }
            catch (Exception ex)
            {
                //异常处理
                MessageBox.Show(ex.ToString());
            }
        }

使用button2控件的点击事件调用全屏截图的方法:

private void button2_Click_1(object sender, EventArgs e)
        {
            saveB();
        }

运行程序如下:

 

 截取的是当前显示器展示的区域,以左上角开始截取。


四、总结

主要掌握方法,就可以把方法调入到其他的程序中使用。

 完整代码如下:

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.Threading.Tasks;
using System.Windows.Forms;
using VisioForge.MediaFramework.WIN32;

namespace MW1005DEMOJieTu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
        }
        //用于双击截图
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(IntPtr hdcDest, // 目标 DC的句柄
                                          int nXDest,
                                          int nYDest,
                                          int nWidth,
                                          int nHeight,
                                          IntPtr hdcSrc,  // 源DC的句柄
                                          int nXSrc,
                                          int nYSrc,
                                          System.Int32 dwRop  // 光栅的处理数值
                                          );


        private void button1_Click(object sender, EventArgs e)
        {
            saveA();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            saveB();
        }

        //全屏
        public void saveA()
        {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);//获取当前屏幕大小

            Graphics g1 = this.CreateGraphics();//创建一个以当前屏幕为模板的对象
            Image myImage = new Bitmap(rect.Width, rect.Height, g1);//创建以屏幕大小为标准的位图

            Graphics g2 = Graphics.FromImage(myImage);
            IntPtr dc1 = g1.GetHdc();//得到屏幕的dc
            IntPtr dc2 = g2.GetHdc();//得到bitmap的dc
            BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);//实现屏幕捕获
            g1.ReleaseHdc(dc1);//释放屏幕的dc
            g2.ReleaseHdc(dc2);//释放bitmap的dc

            string fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取应用程序运行的路径

            //可以根据需要,把屏幕以其他图片的格式来保存,如想把图片保存为位图文件,可以把"ImageFormat.Jpeg"改换成"ImageFormat.Bmp";
            //想把图片保存为Gif文件,就把"ImageFormat.Jpeg"改换成"ImageFormat.Gif"。可以保存的文件类型大概有十多种
            myImage.Save(filePath + fileName, ImageFormat.Jpeg);
            MessageBox.Show("文件保存在:" + filePath + fileName);
        }

        //指定区域
        private void saveB()
        {
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取应用程序运行的路径
            string fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名
            try
            {
                Screen screen = Screen.AllScreens.FirstOrDefault();//获取当前第一块屏幕(根据需求也可以换其他屏幕)
                //创建需要截取的屏幕区域  
                Rectangle rc = new Rectangle(0, 0, 200, 200);
                //生成截图的位图容器  
                Bitmap bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
                //GDI+图像画布  
                using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
                {
                    memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);//对屏幕指定区域进行图像复制
                }
                bitmap.Save(filePath + fileName, ImageFormat.Png);
                MessageBox.Show("文件保存在:" + filePath + fileName);
            }
            catch (Exception ex)
            {
                //异常处理
                MessageBox.Show(ex.ToString());
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            saveA();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            saveB();
        }

        private void btn_max_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值