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();
}
}
}
}
}
}
}
相关文章推荐
- C#实现带指定文本水印的截图(指定窗口与 全屏)
- 解决C# 截取当前程序窗口指定位置截图的实现方法
- C#中父窗口和子窗口之间实现控件互操作
- C#简单实现窗口全透明
- 利用C#实现任务栏通知窗口
- 用C#实现全屏幕截图
- C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现
- C#调用windows的API函数实现将不活动窗口置为活动窗口,并置前显示
- C#实现的ActiveX截图打印控件
- C#实现窗口最小化到系统托盘
- [C# 开发技巧]实现属于自己的截图工具
- 【转载】用C#语言实现类似QQ的截图功能
- C#浮动的窗口如何实现
- C#父窗口和其他窗口之间实现控件互操作
- C#窗体实现QQ截图功能
- C# 实现窗口"绑架" (转)
- 利用C# 实现任务栏通知窗口
- 【转】C# 实现完整功能的截图控件(2)-实现颜色和字体选择控件
- C#获取进程的主窗口句柄的实现方法
- C#实现窗口之间的传值