C# 实现窗口截图
注意要自己添加必要的引用哦
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Diagnostics; using System.Runtime.InteropServices; namespace Console1 { class Program { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [STAThread] static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Standard Enumeration Format Specifiers"); Console.WriteLine( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Process proc = new Process(); proc.StartInfo.FileName = "notepad.exe"; proc.Start(); proc.WaitForInputIdle(); if (SetForegroundWindow(proc.MainWindowHandle)) { RECT srcRect; if (!proc.MainWindowHandle.Equals(IntPtr.Zero)) { if (GetWindowRect(proc.MainWindowHandle, out srcRect)) { int width = srcRect.Right - srcRect.Left; int height = srcRect.Bottom - srcRect.Top; Bitmap bmp = new Bitmap(width, height); Graphics screenG = Graphics.FromImage(bmp); try { screenG.CopyFromScreen(srcRect.Left, srcRect.Top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy); bmp.Save("notepad.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { //MessageBox.Show(ex.Message); Console.WriteLine("Exception caught: " + ex.ToString()); } finally { screenG.Dispose(); bmp.Dispose(); } } } } } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Console1
{
class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[STAThread]
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
if (SetForegroundWindow(proc.MainWindowHandle))
{
RECT srcRect;
if (!proc.MainWindowHandle.Equals(IntPtr.Zero))
{
if (GetWindowRect(proc.MainWindowHandle, out srcRect))
{
int width = srcRect.Right - srcRect.Left;
int height = srcRect.Bottom - srcRect.Top;
Bitmap bmp = new Bitmap(width, height);
Graphics screenG = Graphics.FromImage(bmp);
try
{
screenG.CopyFromScreen(srcRect.Left, srcRect.Top,
0, 0, new Size(width, height),
CopyPixelOperation.SourceCopy);
bmp.Save("notepad.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
Console.WriteLine("Exception caught: " + ex.ToString());
}
finally
{
screenG.Dispose();
bmp.Dispose();
}
}
}
}
}
}
}
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Console1
{
class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[STAThread]
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
if (SetForegroundWindow(proc.MainWindowHandle))
{
RECT srcRect;
if (!proc.MainWindowHandle.Equals(IntPtr.Zero))
{
if (GetWindowRect(proc.MainWindowHandle, out srcRect))
{
int width = srcRect.Right - srcRect.Left;
int height = srcRect.Bottom - srcRect.Top;
Bitmap bmp = new Bitmap(width, height);
Graphics screenG = Graphics.FromImage(bmp);
try
{
screenG.CopyFromScreen(srcRect.Left, srcRect.Top,
0, 0, new Size(width, height),
CopyPixelOperation.SourceCopy);
bmp.Save("notepad.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
Console.WriteLine("Exception caught: " + ex.ToString());
}
finally
{
screenG.Dispose();
bmp.Dispose();
}
}
}
}
}
}
}