不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
WinForm屏幕/窗体/控件截图方法[转]
编辑:dnawo 日期:2020-02-22
复制内容到剪贴板
程序代码

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FederalCredit
{
public class MZCapture
{
#region DLL calls
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
/// <summary>
/// 屏幕截图(全屏)
/// </summary>
/// <returns></returns>
public static Bitmap CaptureScreen()
{
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
return CaptureScreen(0, 0, screenSize.Width, screenSize.Height);
}
/// <summary>
/// 屏幕截图(指定区域)
/// </summary>
/// <param name="x">左上角的横坐标</param>
/// <param name="y">左上角的纵坐标</param>
/// <param name="width">抓取宽度</param>
/// <param name="height">抓取高度</param>
/// <returns></returns>
public static Bitmap CaptureScreen(int x, int y, int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
g.Dispose();
}
return bmp;
}
/// <summary>
/// 控件截图
/// </summary>
/// <param name="control">需要被截图的控件</param>
/// <remarks>使用BitBlt方法,控件被遮挡时也可以正确截图</remarks>
public static Bitmap CaptureControl(Control control)
{
IntPtr hSrce = GetWindowDC(control.Handle);
IntPtr hDest = CreateCompatibleDC(hSrce);
IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
IntPtr hOldBmp = SelectObject(hDest, hBmp);
if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
{
Bitmap bmp = Image.FromHbitmap(hBmp);
SelectObject(hDest, hOldBmp);
DeleteObject(hBmp);
DeleteDC(hDest);
ReleaseDC(control.Handle, hSrce);
// bmp.Save(@"a.png");
// bmp.Dispose();
return bmp;
}
return null;
}
/// <summary>
/// 窗体截图
/// </summary>
/// <param name="control">需要被截图的窗口</param>
/// <remarks>使用PrintWindow方法,被遮挡时也可以正确截图</remarks>
public static Bitmap CaptureForm(Form form)
{
return GetWindow(form.Handle);
}
private static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return bmp;
}
}
}
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FederalCredit
{
public class MZCapture
{
#region DLL calls
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
/// <summary>
/// 屏幕截图(全屏)
/// </summary>
/// <returns></returns>
public static Bitmap CaptureScreen()
{
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
return CaptureScreen(0, 0, screenSize.Width, screenSize.Height);
}
/// <summary>
/// 屏幕截图(指定区域)
/// </summary>
/// <param name="x">左上角的横坐标</param>
/// <param name="y">左上角的纵坐标</param>
/// <param name="width">抓取宽度</param>
/// <param name="height">抓取高度</param>
/// <returns></returns>
public static Bitmap CaptureScreen(int x, int y, int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
g.Dispose();
}
return bmp;
}
/// <summary>
/// 控件截图
/// </summary>
/// <param name="control">需要被截图的控件</param>
/// <remarks>使用BitBlt方法,控件被遮挡时也可以正确截图</remarks>
public static Bitmap CaptureControl(Control control)
{
IntPtr hSrce = GetWindowDC(control.Handle);
IntPtr hDest = CreateCompatibleDC(hSrce);
IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
IntPtr hOldBmp = SelectObject(hDest, hBmp);
if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
{
Bitmap bmp = Image.FromHbitmap(hBmp);
SelectObject(hDest, hOldBmp);
DeleteObject(hBmp);
DeleteDC(hDest);
ReleaseDC(control.Handle, hSrce);
// bmp.Save(@"a.png");
// bmp.Dispose();
return bmp;
}
return null;
}
/// <summary>
/// 窗体截图
/// </summary>
/// <param name="control">需要被截图的窗口</param>
/// <remarks>使用PrintWindow方法,被遮挡时也可以正确截图</remarks>
public static Bitmap CaptureForm(Form form)
{
return GetWindow(form.Handle);
}
private static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return bmp;
}
}
}
原文链接:https://www.iteye.com/blog/cherishlc-1687738
评论: 0 | 引用: 0 | 查看次数: 3732
发表评论
请登录后再发表评论!