BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图

摘要:
BitBlt该函数对指定的源设备环境区域中的像素进行位块转换,以传送到目标设备环境。DSTINVERT:表示使目标矩形区域颜色取反。dwRopValuesFromwingdi.h:#defineBLACKNESS0x42#defineDSTINVERT0x550009#defineMERGECOPY0xC000CA#defineMERGEPAINT0xBB0226#defineNOTSRCCOPY0x330008#defineNOTSRCERASE0x1100A6#definePATCOPY0xF00021#definePATINVERT0x5A0049#definePATPAINT0xFB0A09#defineSRCAND0x8800C6#defineSRCCOPY0xCC0020#defineSRCERASE0x440328#defineSRCINVERT0x660046#defineSRCPAINT0xEE0086#defineWHITENESS0xFF0062返回值如果函数成功,那么返回值非零;如果函数失败,则返回值为零。WindowsNT:若想获取更多错误信息,请调用GetLastError函数。如果源和目标设备环境代表不同的设备,那么BitBlt函数返回错误。WindowsCE:在WindowsCE1.0版中,参数dwRop只可以指定为下列值:SRCCOPY、SRCAND、SRCPAINT、SRCINVERT。在WindowsCE2.0版中,参数dwRop可以是任何光栅操作代码值。
BitBlt
该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境

函数原型

            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, intnYDest, int nWidth, intnHeight, 
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 classImageHelper
    {
        public static Bitmap CaptureWindow(IntPtr handle, int width, intheight)
        {
            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, 0, 0, width, height, hdcSrc, 0, 0, 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);
                returnimg;
            }
            catch(Exception ex)
            {
                LogHelper.Execption(ex, nameof(ImageHelper));
            }
            return null;
        }
        public static Bitmap CaptureWindow(IntPtr handle, int widthSrc, int heightSrc, int widthDest, intheightDest)
        {
            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, 0, 0, outpoint);
                //bitblt over
                GDI32.StretchBlt(hdcDest, 0, 0, widthDest, heightDest, hdcSrc, 0, 0, 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);
                returnimg;
            }
            catch(Exception ex)
            {
                LogHelper.Execption(ex, nameof(ImageHelper));
            }
            return null;
        }
        /// <summary>  
        ///Helper class containing Gdi32 API functions  
        /// </summary>  
        public classGDI32
        {
            public const int CAPTUREBLT = 1073741824;
            public const int SRCCOPY = 0x00CC0020; //BitBlt dwRop parameter  
            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, intnYDest,
                int nWidth, intnHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, intdwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, intnWidth,
                intnHeight);
            [DllImport("gdi32.dll")]
            public static externIntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern boolDeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern boolDeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static externIntPtr 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, intnHeightDest,
                IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, intdwRop);
            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, intiStretchMode);
            [StructLayout(LayoutKind.Sequential)]
            public structPOINTAPI
            {
                public intx;
                public inty;
            }
            [DllImport("gdi32.dll")]
            public static extern bool SetBrushOrgEx(IntPtr hdc, int nXOrg, int nYOrg, outPOINTAPI lppt);
        }
        /// <summary>  
        ///Helper class containing User32 API functions  
        /// </summary>  
        public classUser32
        {
            [StructLayout(LayoutKind.Sequential)]
            public structRECT
            {
                public intleft;
                public inttop;
                public intright;
                public intbottom;
            }
            [DllImport("user32.dll")]
            public static externIntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static externIntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static externIntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, refRECT rect);
            public const int WM_PAINT = 0x000F;
            [DllImport("user32.dll", EntryPoint = "SendMessageA")]
            public static extern uint SendMessage(IntPtr hWnd, intwMsg, IntPtr wParam, IntPtr lParam);
            [DllImport("user32.dll")]
            public static extern boolPrintWindow(
                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, intnCmdShow);
            #region  窗口关联
            //nCmdShow的含义
            //0 关闭窗口
            //1 正常大小显示窗口
            //2 最小化窗口
            //3 最大化窗口
            //使用实例: ShowWindow(myPtr, 0);
            #endregion
        }
    }
按句柄截图、直接截取缩略图Demo
StretchBltSetStretchBltModeSetBrushOrgEx
参考资料:百度百科随笔MSDN
延展阅读:MSDN Bitmap Functions

免责声明:文章转载自《BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue 阿里云上传组件IOS 多线程的一些总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C# 关键字extern用法

修饰符用于声明在外部实现的方法。extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static,如下面的示例所示:[DllImport("avifil32.dll")]private static extern void AVIFileInit();注意ext...

python中bitmap的参数

Tkinter Bitmaps: 你会使用这个属性显示一个位图。有以下类型的可用位图. 你会使用这个属性显示一个位图。有以下类型的可用位图.: "error" "gray75" "gray50" "gray25" "gray12" "hourglass" "info" "questhead" "question" "warning"...

C# api调用,上窗口在最上面,模拟键盘

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime....

【DLL相关】实现函数的DLL封装,并在另一个项目中调用

直接给出步骤: ===========函数的DLL封装=========== 1.创建第一个项目:win32控制台程序,应用程序类型:DLL,附加选项:导出符号(命名:double_dll) 2.double_dll.h中加入函数定义   extern DOUBLE_DLL_API int doublefun(int);//DOUBLE_DLL_API 根...

自己写的一个随机生成ID号的函数(C#)

public string GetRandomNum(int num_down , int num_up){ //传递随机数的上下限 用于限制其长度 注意 num_up的值上限1000000000int re=0; Random ro=new Random(unchecked((int)DateTime.Now.Ticks));re=ro.Next(num...

[ext4]03 磁盘布局 – Flexible group分析

Flexible Block Groups (flex_bg),我称之为“弹性块组”,是EXT4文件系统引入的一个feature。 所谓Flexible Block Groups,就是将连续的多个物理block groups绑在一起组成一个逻辑块组,这个逻辑块组就称之为Flex_group(也就是flex_bg)。 在一个Flex_group中,第一个物理...