第一步當然就是擷取畫面
執行結果:
程式碼:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
/// <summary>
/// 範圍
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
/// <summary>
/// 取得應用程式畫面
/// </summary>
/// <param name="hWnd">程序</param>
/// <param name="bounds">範圍</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern Boolean GetWindowRect(IntPtr hWnd, ref Rect rect);
/// <summary>
/// 將程式至於前景
/// </summary>
/// <param name="hWnd">程序</param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 顯示視窗
/// </summary>
/// <param name="hWnd">程序</param>
/// <param name="nCmdShow">命令</param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var process = Process.GetProcessesByName("player");
foreach (var p in process)
{
SetForegroundWindow(p.MainWindowHandle);
ShowWindow(p.MainWindowHandle, 1);
Thread.Sleep(500);
Rect rect = new Rect();
GetWindowRect(p.MainWindowHandle, ref rect);
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics.FromImage(bmp).CopyFromScreen(rect.Left,
rect.Top,
0,
0,
new Size(width, height),
CopyPixelOperation.SourceCopy);
string path = DateTime.Now.ToString("yyyyMMdd HHmmss") + ".jpg";
bmp.Save(path);
}
}
}
}

