求C#截图代码

FXL629 2009-11-08 10:59:45
希望求得C#截图代码,希望哪位大虾能指点!!如果能指导在下那是最好~~呵呵~~
...全文
367 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nick1yong 2011-04-20
  • 打赏
  • 举报
回复
good
FXL629 2009-11-09
  • 打赏
  • 举报
回复
有没有说的详细点的 我是菜鸟~~
fengling2001 2009-11-09
  • 打赏
  • 举报
回复

/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}

/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get the hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest,hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);

// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);

return img;
}

/// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename,format);
}

/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename,format);
}

/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
}

/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}
wei840120 2009-11-08
  • 打赏
  • 举报
回复
同问啊
懦芞 2009-11-08
  • 打赏
  • 举报
回复
3楼的和我说的是一个地址。
懦芞 2009-11-08
  • 打赏
  • 举报
回复
前段时间有人发这个的源码。
http://topic.csdn.net/u/20090911/14/5614a16e-ab08-4106-901e-e8dcc5cd1fa3.html
fengling2001 2009-11-08
  • 打赏
  • 举报
回复
网络上多得是,自己搜索下,有很多写成类,自己调用就可以
wuyq11 2009-11-08
  • 打赏
  • 举报
回复
Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Image img = new Bitmap(R.Width, R.Height);
Graphics G = Graphics.FromImage(img);
G.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(R.Width, R.Height));。
IntPtr dc = G.GetHdc();
G.ReleaseHdc(dc);
G.Dispose();
img .Save("c:\\a.jpg");

private static extern bool BitBlt(IntPtr hdcDest,int nXDest,int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,Int32 dwrop);

http://topic.csdn.net/u/20090911/14/5614a16e-ab08-4106-901e-e8dcc5cd1fa3.html
wushaokai 2009-11-08
  • 打赏
  • 举报
回复
Bitmap pic = new Bitmap(width,height);
Graphics g = Graphics.FromImage(pic);
g.CopyFromScreen(new Point(rect.left, rect.top), new Point(0, 0), new Size(width, height));
这是截全屏的图,看一下Graphics方法CopyFromScreen方法就知道了
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090911/14/5614a16e-ab08-4106-901e-e8dcc5cd1fa3.html

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧