標籤:windows 圖片 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;
}
}
}
}
}
C#螢幕拷貝源碼