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


         
        }




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