#region 使用api32 根据窗体句柄进行截图
/// <summary>
/// 此方法仅仅适用于设计器画在pan上的
/// </summary>
/// <param name="cot"></param>
/// <returns></returns>
public Bitmap CutControlBitmap(Control cot)
{
Bitmap bmp = new Bitmap(cot.Width, cot.Height);
cot.DrawToBitmap(bmp, cot.ClientRectangle);
return bmp;
}
public Bitmap CutControlBitmap(IntPtr winCotPtr)
{
//1先获取控件的大小
IntPtr hscrdc = GetWindowDC(winCotPtr);
RECT rECT = new RECT();
GetWindowRect(winCotPtr, ref rECT);
IntPtr mapPtr = CreateCompatibleBitmap(hscrdc, rECT.Right - rECT.Left, rECT.Bottom - rECT.Top);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, mapPtr);
PrintWindow(winCotPtr, hmemdc, 0);
Bitmap bmp=Bitmap.FromHbitmap(mapPtr);
DeleteObject(mapPtr);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return bmp;
}
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[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 IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
[DllImport("gdi32.dll")]
public static extern IntPtr DeleteObject(IntPtr hgdiobj);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hdc);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
#endregion
C# 根据控件 或者操作句柄进行截图 备忘
最新推荐文章于 2024-10-28 09:43:40 发布
这个博客介绍了如何利用API32函数在Windows环境下,通过窗体句柄实现对控件或窗体的截图。提供了两种方法,一种是针对设计器画在Pan上的控件,另一种是直接通过Win32 API获取并绘制窗口内容。涉及到的关键函数包括GetWindowDC, CreateCompatibleBitmap, PrintWindow等。
2万+

被折叠的 条评论
为什么被折叠?



