QQ截图的核心其实就是调用WINDOWS API函数,主要涉及两个核心组件, user32.dll和gdi32.dll。
如下是,C#代码调用上述两个核心组件的完整示例:
namespace WindowsFormsApplication1{
/// <summary>
/// 屏幕捕获类
/// </summary>
public class ScreenCapture
{
/// <summary>
/// 创建一个包含整个桌面的截图Image对象(捕获到的桌面是当前WINDOWS *** 作系统活动桌面)
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow())
}
/// <summary>
/// 创建一个包含特定窗口的截图Image对象
改茄 /// </summary>
/// <param name="handle">启动本程序的句柄窗口(在Windows上这是由Handle属性获得)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// 获取目标窗口的HDC
IntPtr hdcSrc = User32.GetWindowDC(handle)
// 获取它的大小
User32.RECT windowRect = new User32.RECT()
User32.GetWindowRect(handle, ref windowRect)
int width = windowRect.right - windowRect.left
int height = windowRect.bottom - windowRect.top
// 创建设备上下文对象
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc)
橘歼迟 IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap)
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY)
GDI32.SelectObject(hdcDest, hOld)
GDI32.DeleteDC(hdcDest)
User32.ReleaseDC(handle, hdcSrc)
圆李 Image img = Image.FromHbitmap(hBitmap)
GDI32.DeleteObject(hBitmap)
return img
}
/// <summary>
/// 获取特定窗口,并保存它
/// </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>
/// 捕获整个windows活动窗口并保存它
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen()
img.Save(filename, format)
}
/// <summary>
/// GDI32 相关的API函数
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020
[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>
/// User32 API相关函数
/// </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)
}
}
}
// 调用示例:
private void button1_Click(object sender, EventArgs e)
{
ScreenCapture sc = new ScreenCapture()
// 捕获整个屏幕并保存到一个文件里
Image img = sc.CaptureScreen()
// 将捕获的图片显示在图片控件里
this.pictureBox1.Image = img
// 捕获当前运行窗体并保存在C盘,文件名和后缀为temp.png
sc.CaptureWindowToFile(this.Handle, "C:\\temp.png", ImageFormat.Gif)
}
安卓系统手机 *** 作系统相似,以华为手机为例,以下截屏方法您可以试一下:一、屏幕下拉出现“开关”毁李,点击“截屏”即可截图成功(如默认快捷开关没有截屏选项,需要点击编辑添加后方能使用)。
二、“截图”快捷键:同时按住“音量减”和“电源键”也可以实现截图。
三、通过指关节截屏:
1、截取完整屏幕:亮屏状态下连续态乎敲击屏幕两纤闭迟下待d窗自动退出即可完成截屏。
2、截取部分屏幕:亮屏状态下连续敲击屏幕两下,d窗出现三秒内点击编辑,拖拽图片边框即可完成截屏。
3、画字母S滚动截屏:先用指关节敲击一下屏幕,然后不要离开屏幕,直接用指关节画字母S,点击滚动区域后完成截屏。
截屏成功后,图片文件默认保存在手机存储的Pictures/Screenshots文件夹下,也可以通过手机自带的图库查看。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)