您的位置:首页 > 编程语言 > C#

C# 实现窗口截图

2012-02-16 17:09 274 查看
C#实现窗口截图
注意要自己添加必要的引用哦
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Diagnostics;
usingSystem.Runtime.InteropServices;

namespaceConsole1
{
classProgram
{
[DllImport("user32.dll")]
[return:MarshalAs(UnmanagedType.Bool)]
privatestaticexternboolGetWindowRect(IntPtrhWnd,outRECTlpRect);
[DllImport("user32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]
[return:MarshalAs(UnmanagedType.Bool)]
privatestaticexternboolSetForegroundWindow(IntPtrhWnd);
[StructLayout(LayoutKind.Sequential)]

publicstructRECT
{
publicintLeft;
publicintTop;
publicintRight;
publicintBottom;
}

[STAThread]
staticvoidMain(string[]args)
{
Console.ForegroundColor=ConsoleColor.Magenta;
Console.WriteLine("StandardEnumerationFormatSpecifiers");
Console.WriteLine(
"(G)General:.........{0:G}\n"+
"(default):........{0}(default='G')\n"+
"(F)Flags:..........{0:F}(flagsorinteger)\n"+
"(D)Decimalnumber:.....{0:D}\n"+
"(X)Hexadecimal:.......{0:X}\n",
Color.Green);

Processproc=newProcess();
proc.StartInfo.FileName="notepad.exe";
proc.Start();
proc.WaitForInputIdle();
if(SetForegroundWindow(proc.MainWindowHandle))
{
RECTsrcRect;
if(!proc.MainWindowHandle.Equals(IntPtr.Zero))
{
if(GetWindowRect(proc.MainWindowHandle,outsrcRect))
{
intwidth=srcRect.Right-srcRect.Left;
intheight=srcRect.Bottom-srcRect.Top;

Bitmapbmp=newBitmap(width,height);
GraphicsscreenG=Graphics.FromImage(bmp);

try
{
screenG.CopyFromScreen(srcRect.Left,srcRect.Top,
0,0,newSize(width,height),
CopyPixelOperation.SourceCopy);

bmp.Save("notepad.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message);
Console.WriteLine("Exceptioncaught:"+ex.ToString());
}
finally
{
screenG.Dispose();
bmp.Dispose();
}
}
}
}

}
}
}



usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Drawing;

usingSystem.Diagnostics;

usingSystem.Runtime.InteropServices;

namespaceConsole1

{

classProgram

{

[DllImport("user32.dll")]

[return:MarshalAs(UnmanagedType.Bool)]

privatestaticexternboolGetWindowRect(IntPtrhWnd,outRECTlpRect);

[DllImport("user32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]

[return:MarshalAs(UnmanagedType.Bool)]

privatestaticexternboolSetForegroundWindow(IntPtrhWnd);

[StructLayout(LayoutKind.Sequential)]

publicstructRECT

{

publicintLeft;

publicintTop;

publicintRight;

publicintBottom;

}

[STAThread]

staticvoidMain(string[]args)

{

Console.ForegroundColor=ConsoleColor.Magenta;

Console.WriteLine("StandardEnumerationFormatSpecifiers");

Console.WriteLine(

"(G)General:.........{0:G}\n"+

"(default):........{0}(default='G')\n"+

"(F)Flags:..........{0:F}(flagsorinteger)\n"+

"(D)Decimalnumber:.....{0:D}\n"+

"(X)Hexadecimal:.......{0:X}\n",

Color.Green);

Processproc=newProcess();

proc.StartInfo.FileName="notepad.exe";

proc.Start();

proc.WaitForInputIdle();

if(SetForegroundWindow(proc.MainWindowHandle))

{

RECTsrcRect;

if(!proc.MainWindowHandle.Equals(IntPtr.Zero))

{

if(GetWindowRect(proc.MainWindowHandle,outsrcRect))

{

intwidth=srcRect.Right-srcRect.Left;

intheight=srcRect.Bottom-srcRect.Top;

Bitmapbmp=newBitmap(width,height);

GraphicsscreenG=Graphics.FromImage(bmp);

try

{

screenG.CopyFromScreen(srcRect.Left,srcRect.Top,

0,0,newSize(width,height),

CopyPixelOperation.SourceCopy);

bmp.Save("notepad.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

}

catch(Exceptionex)

{

//MessageBox.Show(ex.Message);

Console.WriteLine("Exceptioncaught:"+ex.ToString());

}

finally

{

screenG.Dispose();

bmp.Dispose();

}

}

}

}

}

}

}

System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Drawing;

usingSystem.Diagnostics;

usingSystem.Runtime.InteropServices;

namespaceConsole1

{

classProgram

{

[DllImport("user32.dll")]

[return:MarshalAs(UnmanagedType.Bool)]

privatestaticexternboolGetWindowRect(IntPtrhWnd,outRECTlpRect);

[DllImport("user32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]

[return:MarshalAs(UnmanagedType.Bool)]

privatestaticexternboolSetForegroundWindow(IntPtrhWnd);

[StructLayout(LayoutKind.Sequential)]

publicstructRECT

{

publicintLeft;

publicintTop;

publicintRight;

publicintBottom;

}

[STAThread]

staticvoidMain(string[]args)

{

Console.ForegroundColor=ConsoleColor.Magenta;

Console.WriteLine("StandardEnumerationFormatSpecifiers");

Console.WriteLine(

"(G)General:.........{0:G}\n"+

"(default):........{0}(default='G')\n"+

"(F)Flags:..........{0:F}(flagsorinteger)\n"+

"(D)Decimalnumber:.....{0:D}\n"+

"(X)Hexadecimal:.......{0:X}\n",

Color.Green);

Processproc=newProcess();

proc.StartInfo.FileName="notepad.exe";

proc.Start();

proc.WaitForInputIdle();

if(SetForegroundWindow(proc.MainWindowHandle))

{

RECTsrcRect;

if(!proc.MainWindowHandle.Equals(IntPtr.Zero))

{

if(GetWindowRect(proc.MainWindowHandle,outsrcRect))

{

intwidth=srcRect.Right-srcRect.Left;

intheight=srcRect.Bottom-srcRect.Top;

Bitmapbmp=newBitmap(width,height);

GraphicsscreenG=Graphics.FromImage(bmp);

try

{

screenG.CopyFromScreen(srcRect.Left,srcRect.Top,

0,0,newSize(width,height),

CopyPixelOperation.SourceCopy);

bmp.Save("notepad.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

}

catch(Exceptionex)

{

//MessageBox.Show(ex.Message);

Console.WriteLine("Exceptioncaught:"+ex.ToString());

}

finally

{

screenG.Dispose();

bmp.Dispose();

}

}

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: