BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图
函数原型
[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);
参数
返回值
按句柄截图 、直接截取缩略图
public static class ImageHelper
{ public static Bitmap CaptureWindow(IntPtr handle, int width, int height)
{
try
{
// get the hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// 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, , , width, height, hdcSrc, , , GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it
Bitmap img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap); return img;
}
catch (Exception ex)
{
LogHelper.Execption(ex, nameof(ImageHelper));
}
return null;
} public static Bitmap CaptureWindow(IntPtr handle, int widthSrc, int heightSrc, int widthDest, int heightDest)
{
try
{
// get the hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// 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, widthDest, heightDest);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.SetStretchBltMode(hdcDest, GDI32.STRETCH_HALFTONE); GDI32.POINTAPI point;
GDI32.SetBrushOrgEx(hdcDest, , , out point); // bitblt over
GDI32.StretchBlt(hdcDest, , , widthDest, heightDest, hdcSrc, , , widthSrc, heightSrc, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it
Bitmap img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap); return img;
}
catch (Exception ex)
{
LogHelper.Execption(ex, nameof(ImageHelper));
}
return null;
} /// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
public class GDI32
{
public const int CAPTUREBLT = ;
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>
/// 缩放截图
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXOriginDest"></param>
/// <param name="nYOriginDest"></param>
/// <param name="nWidthDest"></param>
/// <param name="nHeightDest"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXOriginSrc"></param>
/// <param name="nYOriginSrc"></param>
/// <param name="nWidthSrc"></param>
/// <param name="nHeightSrc"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
[DllImport("gdi32.dll")]
public static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int dwRop); public const int STRETCH_ANDSCANS = 0x01;
public const int STRETCH_ORSCANS = 0x02;
public const int STRETCH_DELETESCANS = 0x03;
public const int STRETCH_HALFTONE = 0x04; /// <summary>
/// 设置缩放模式
/// </summary>
/// <param name="hdc"></param>
/// <param name="iStretchMode"></param>
/// <returns>失败返回0</returns>
[DllImport("gdi32.dll")]
public static extern int SetStretchBltMode(IntPtr hdc, int iStretchMode); [StructLayout(LayoutKind.Sequential)]
public struct POINTAPI
{
public int x;
public int y;
} [DllImport("gdi32.dll")]
public static extern bool SetBrushOrgEx(IntPtr hdc, int nXOrg, int nYOrg, out POINTAPI lppt); } /// <summary>
/// Helper class containing User32 API functions
/// </summary>
public 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); public const int WM_PAINT = 0x000F;
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern uint SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd, // Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, // HDC to print into,Handle to the device context.
UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values.
); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); #region 窗口关联
// nCmdShow的含义
//0 关闭窗口
//1 正常大小显示窗口
//2 最小化窗口
//3 最大化窗口
//使用实例: ShowWindow(myPtr, 0);
#endregion
} }
按句柄截图、直接截取缩略图 Demo
BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图的更多相关文章
- malloc 与 free函数详解<转载>
malloc和free函数详解 本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...
- NSSearchPathForDirectoriesInDomains函数详解
NSSearchPathForDirectoriesInDomains函数详解 #import "NSString+FilePath.h" @implementation ...
- JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解
二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...
- Linux C popen()函数详解
表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...
- kzalloc 函数详解(转载)
用kzalloc申请内存的时候, 效果等同于先是用 kmalloc() 申请空间 , 然后用 memset() 来初始化 ,所有申请的元素都被初始化为 0. view plain /** * kzal ...
- Netsuite Formula > Oracle函数列表速查(PL/SQL单行函数和组函数详解).txt
PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句,函数主要分为两大类: 单行函数 ...
- jQuery.attr() 函数详解
一,jQuery.attr() 函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...
- memset函数详解
语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关 功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...
- CreateFile函数详解
CreateFile函数详解 CreateFile The CreateFile function creates or opens the following objects and returns ...
随机推荐
- 我今天遇到的条件语句Integer类型的
两个Integer类型的值进行比较时,应该用equals进行判断,用"=="判断是错误的,后来想了一下就明白了,Integer毕竟是对象, 而不是int基本数据类型,可以直接比较, ...
- Stall Reservations POJ - 3190(贪心)
Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...
- Codeforces 854C Planning 【贪心】
<题目链接> 题目大意: 表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟 ...
- 835.Hamming距离
描述 两个整数的Hamming距离是对应比特位不同的个数. 给定两个整数x和y,计算两者的Hamming距离. 0 ≤ x, y < 2^31. 您在真实的面试中是否遇到过这个题? 样例 输入: ...
- VBA调用DOS程序两种方法
Set wsh = VBA.CreateObject("WScript.Shell") 'wsh.Run strExePath & " g", vbHi ...
- 现阶段如何开始使用v-ray for unrealengine
如何安装 因为现阶段v-ray for unrealengine还处于beta测试阶段所以你必须去https://www.chaosgroup.com/vray/unreal 申请测试资格申请完之后就 ...
- Python应用【PDF处理-pypdf2】
概述 Python处理PDF文件需要安装相应的库:[PyPDF2]库 使用场景 工作中可能会涉及处理pdf文件,PyPDF2就是这样一个库, 使用它可以轻松的处理 pdf 文件,它提供了读.写.分割. ...
- 2004 ACM 成绩转换 两种方法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2004 中文题目,简单题. 题意:将分数转换成ABC制 查表法 #include <stdio.h&g ...
- java的类和对象
创建狗狗类: /** * 狗狗类 * @author Administrator * */ public class Dog { String name="无名氏"; //姓名 i ...
- Ubuntu16.04搜狗输入法无法输入中文
搜狗输入法图标显示正常,但是无法输入中文,切换为中文输入时无待选中文,只能输入英文. 这种情况应该是搜狗输入法的配置出现了问题,因为重装输入法时配置文件夹会保留,因此重装无法解决问题. 解决方案: 1 ...