Winform截图

您所在的位置:网站首页 unitygui控件截图 Winform截图

Winform截图

2023-03-13 11:34| 来源: 网络整理| 查看: 265

本文属C#系列文章目录

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

目录

2023/3/10更新内容:

正文:

        一、最大化最小化按钮

        二、全屏截取

        三、指定区域截图

        四、总结

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; } } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3