C#屏幕拷贝源码

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


namespace CopyScreen
{
    public partial class MainForm : Form
    {
        System.Windows.Forms.Timer T = new System.Windows.Forms.Timer();


        public MainForm()
        {
            InitializeComponent();
            T.Interval = 1000;
            T.Tick += new EventHandler(T_Tick);
            T.Enabled = false;
        }


        private void T_Tick(object sender, EventArgs e)
        {
            button2_Click(sender, e);
            T.Enabled = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Screen sc = Screen.PrimaryScreen;//取得主屏
            Rectangle rct = sc.Bounds;//得到主屏的范围
            Image img = new Bitmap(rct.Width, rct.Height);
            Graphics gp = Graphics.FromImage(img);
            gp.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(rct.Width, rct.Height));   //关键是这个函数
            img.Save("Test.bmp", System.Drawing.Imaging.ImageFormat.Png);
            img.Dispose();
            gp.Dispose();
            GC.Collect();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = System.Environment.CurrentDirectory + "\\Test.bmp";


            if (File.Exists(fileName))
            {    //建立新的系统进程  
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                //设置文件名,此处为图片的真实路径+文件名  
                process.StartInfo.FileName = fileName;
                //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。  
                process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
                //此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true  
                process.StartInfo.UseShellExecute = true;
                //此处可以更改进程所打开窗体的显示样式,可以不设  
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                process.Start();
                process.Close();


                process.Dispose();
                GC.Collect();
            }
            else
            {
                if (MessageBox.Show("The picture does not exist, whether to copy the screen?", "Information:", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    Thread.Sleep(500);
                    button1_Click(sender, e);


                    T.Enabled = true;
                }
            }


         
        }




    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章