BitBlt

该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境

函数原型

            [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);

参数

hDestDC:指向目标设备环境的句柄
x:指定目标矩形区域左上角的X轴逻辑坐标。
y:指定目标矩形区域左上角的Y轴逻辑坐标。
nWidth:指定源在目标矩形区域的逻辑宽度。
nHeight:指定源在目标矩形区域的逻辑高度。
hSrcDC:指向源设备环境的句柄。
xSrc:指定源矩形区域左上角的X轴逻辑坐标。
ySrc:指定源矩形区域左上角的Y轴逻辑坐标。
dwRop:指定光栅操作代码。这些代码将定义源矩形区域的颜色数据,如何与目标矩形区域的颜色数据组合以完成最后的颜色。
 
下面列出了一些常见的光栅操作代码:
 
BLACKNESS:表示使用与物理调色板的索引0相关的色彩来填充目标矩形区域,(对缺省的物理调色板而言,该颜色为黑色)。
DSTINVERT:表示使目标矩形区域颜色取反。
MERGECOPY:表示使用布尔型的AND(与)操作符将源矩形区域的颜色与特定模式组合一起。
MERGEPAINT:通过使用布尔型的OR(或)操作符将反向的源矩形区域的颜色与目标矩形区域的颜色合并。
NOTSRCCOPY:将源矩形区域颜色取反,于拷贝到目标矩形区域。
NOTSRCERASE:使用布尔类型的OR(或)操作符组合源和目标矩形区域的颜色值,然后将合成的颜色取反。
PATCOPY:将特定的模式拷贝到目标位图上。
PATPAINT:通过使用布尔OR(或)操作符将源矩形区域取反后的颜色值与特定模式的颜色合并。然后使用OR(或)操作符将该操作的结果与目标矩形区域内的颜色合并。
PATINVERT:通过使用XOR(异或)操作符将源和目标矩形区域内的颜色合并。
SRCAND:通过使用AND(与)操作符来将源和目标矩形区域内的颜色合并。
SRCCOPY:将源矩形区域直接拷贝到目标矩形区域。
SRCERASE:通过使用AND(与)操作符将目标矩形区域颜色取反后与源矩形区域的颜色值合并。
SRCINVERT:通过使用布尔型的XOR(异或)操作符将源和目标矩形区域的颜色合并。
SRCPAINT:通过使用布尔型的OR(或)操作符将源和目标矩形区域的颜色合并。
WHITENESS:使用与物理调色板中索引1有关的颜色填充目标矩形区域。(对于缺省物理调色板来说,这个颜色就是白色)。
dwRop Values
From wingdi.h:
#define BLACKNESS 0x42
#define DSTINVERT 0x550009
#define MERGECOPY 0xC000CA
#define MERGEPAINT 0xBB0226
#define NOTSRCCOPY 0x330008
#define NOTSRCERASE 0x1100A6
#define PATCOPY 0xF00021
#define PATINVERT 0x5A0049
#define PATPAINT 0xFB0A09
#define SRCAND 0x8800C6
#define SRCCOPY 0xCC0020
#define SRCERASE 0x440328
#define SRCINVERT 0x660046
#define SRCPAINT 0xEE0086
#define WHITENESS 0xFF0062
 

返回值

如果函数成功,那么返回值非零;如果函数失败,则返回值为零。
Windows NT:若想获取更多错误信息,请调用GetLastError函数。
备注:如果在源设备环境中可以实行旋转或剪切变换,那么函数BitBlt返回一个错误。如果存在其他变换(并且目标设备环境中匹配变换无效),那么目标设备环境中的矩形区域将在需要时进行拉伸、压缩或旋转。
如果源和目标设备环境的颜色格式不匹配,那么BitBlt函数将源场景的颜色格式转换成能与目标格式匹配的格式。当正在记录一个增强型图元文件时,如果源设备环境标识为一个增强型图元文件设备环境,那么会出现错误。如果源和目标设备环境代表不同的设备,那么BitBlt函数返回错误。
Windows CE:在Windows CE 1.0版中,参数dwRop只可以指定为下列值:SRCCOPY、SRCAND、SRCPAINT、SRCINVERT。在Windows CE 2.0版中,参数dwRop可以是任何光栅操作代码值。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;头文件:wingdi.h;库文件:gdi32.lib。
 
 
 

按句柄截图 、直接截取缩略图

    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

 
参考资料: 百度百科  随笔  MSDN 
延展阅读: MSDN Bitmap Functions
 
 
 

BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图的更多相关文章

  1. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  2. NSSearchPathForDirectoriesInDomains函数详解

    NSSearchPathForDirectoriesInDomains函数详解     #import "NSString+FilePath.h" @implementation ...

  3. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  4. Linux C popen()函数详解

    表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...

  5. kzalloc 函数详解(转载)

    用kzalloc申请内存的时候, 效果等同于先是用 kmalloc() 申请空间 , 然后用 memset() 来初始化 ,所有申请的元素都被初始化为 0. view plain /** * kzal ...

  6. Netsuite Formula > Oracle函数列表速查(PL/SQL单行函数和组函数详解).txt

    PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句,函数主要分为两大类: 单行函数 ...

  7. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  8. memset函数详解

    语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关  功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...

  9. CreateFile函数详解

    CreateFile函数详解 CreateFile The CreateFile function creates or opens the following objects and returns ...

随机推荐

  1. TF:Tensorflow结构简单应用,随机生成100个数,利用Tensorflow训练使其逼近已知线性直线的效率和截距—Jason niu

    import os os.environ[' import tensorflow as tf import numpy as np x_data = np.random.rand(100).astyp ...

  2. Pyinstaller打包selenium去除chromedriver黑框问题解决!!!

    Pyinstaller打包selenium去除chromedriver黑框问题解决!!!     问题描述 [1123/101706.932:ERROR:gpu_process_transport_f ...

  3. SQL语句中单引号、双引号和反引号的区分

    反引号 反引号:反引号一般在Esc键的下方,为了区分MySQL的保留字与普通字符而引入的符号. 一般我们建表时都会将表名,库名都加上反引号来保证语句的执行度. 例如: SELECT * FROM `u ...

  4. Codeforces 1076D Edge Deletion 【最短路+贪心】

    <题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...

  5. Dijkstra算法之 Java详解

    转载:http://www.cnblogs.com/skywang12345/ 迪杰斯特拉算法介绍 迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径. 它的主 ...

  6. 11.6 正睿停课训练 Day17

    目录 2018.11.6 正睿停课训练 Day17 A chinese(思路 计数) B physics(单调队列/剪枝 DP) C chemistry(期望 DP) 考试代码 A B C 2018. ...

  7. Python3基础-特别函数(map filter partial reduces sorted)实例学习

    1. 装饰器 关于Python装饰器的讲解,网上一搜有很多资料,有些资料讲的很详细.因此,我不再详述,我会给出一些连接,帮助理解. 探究functools模块wraps装饰器的用途 案例1 impor ...

  8. Scrapy基础(九)————将不定长度的URL进行固定长度写入Item中

    前面讲到将每篇文章的URL写入Item,但是每个url的长度是不同的,可以在Item中设置一个字段怎样使得每个URL的长度相同,这就需要对每个URL进行md5运算,使得长度统一,再加入到设定的Item ...

  9. 什么是C++

    1.到底什么是C++ C++是一门面向对象的程序设计语言. 关键词:程序设计语言,面向对象 程序设计语言是用来书写计算机程序的语言,更形象的说,程序设计语言是用来和计算机“交流的语言.” 面向对象(o ...

  10. No mapping found for HTTP request with URI [/webapp/] in DispatcherServlet with name 'SpringMVC'

    可能有如下几个原因: 1.是否设置了web目录,在IDEA中,web目录是这样的 如果没有设置,按照下面的方法设置: 选中要设置的模块,点击file.project structure,设置web.x ...