C#截取当前屏幕界面作为图片保存

来源:互联网 发布:维普数据库论文检测 编辑:程序博客网 时间:2024/04/29 20:46
[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace FormCopy  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         //截屏幕方法  
  19.         private void FormCpy()  
  20.         {  
  21.             string sFileName = "";  
  22.             string sPath = @"E:/Test/Image/";  
  23.             Random rnd = new Random();  
  24.             int width = 0;  
  25.             int heigh = 0;  
  26.             width = this.Size.Width;  
  27.             heigh = this.Size.Height;  
  28.             Bitmap bmp = new Bitmap(width, heigh);  
  29.   
  30.             System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();  
  31.             sFileName =sPath + DateTime.Now.ToString("yyyyMMddHHmmss_") + ass.GetName().Name + "_" + this.Name + "_" + rnd.Next(999) + ".jpg";  
  32.   
  33.             this.DrawToBitmap(bmp, new Rectangle(0, 0, width, heigh));  
  34.             bmp.Save(sFileName,System.Drawing.Imaging.ImageFormat.Jpeg);  
  35.         }  
  36.   
  37.         private void button1_Click(object sender, EventArgs e)  
  38.         {  
  39.             FormCpy();  
  40.         }  
  41.     }  
  42. }  

也可将第34行替换成
 Clipboard.SetImage(bmp);//将图片保存到粘贴板,不保存实际文件。  然后在Word上直接粘贴
0 0