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 ...
随机推荐
- Tomcat启动分析(转自:http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html)
Tomcat启动分析 1 - Tomcat Server的组成部分 1.1 - Server A Server element represents the entire Catalina servl ...
- oracle左连接连表查询
要想把该表的数据全部查出来,必须select中出现该表的字段. SELECT distinct a.ZGSWSKFJ_DM,b.ZGSWJ_DM,b.SSGLY_DM,b.NSRSBH,b.NSRMC ...
- 最佳linux文件WINDOWS上传下载方法
通常,利用SSH管理远程Linux服务器时,经常需要与本地交互文件.当然,我们可以利用FTP方式,比如通过Filezilla客户端软件.不过直接使用SSH软件(SecureCRT.Xshell)自带的 ...
- 《Android高级进阶》读书笔记
<Android高级进阶>是据我所知的市面上唯一一本技术工具书,比较的高大全,作者的目的是为了对全领域有个初步的概念 No1: 在Android系统中,拥有事件传递处理能力的类有以下三种 ...
- 生日蛋糕 POJ - 1190 (搜索+剪枝)
7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当 ...
- Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果
// BaseActivity 是实现的一些公共的变量和方法,可以暂时为空的类 public class HomeActivity extends BaseActivity implements Vi ...
- BZOJ.5285.[AHOI/HNOI2018]寻宝游戏(思路 按位计算 基数排序..)
BZOJ LOJ 洛谷 话说vae去年的专辑就叫寻宝游戏诶 只有我去搜Mystery Hunt和infinite corridor了吗... 同样按位考虑,假设\(m=1\). 我们要在一堆\(01\ ...
- OI常用重载运算符
用STL时经常用到 PS:这里有个巨坑,把重载写在STL里常数会爆炸,分分钟大数据TLE 所以就把重载放在结构体里啦 //以priority_queue的重载为例 //priority_queue默认 ...
- .net 4.0 中的特性总结(四):Tuple类型
Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple Tuple<T> Tuple<T1, T ...
- oracle增加表空间大小
第一步:查看表空间的名字及文件所在位置: select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_sp ...