StretchBlt和StretchDIBits

StretchBlt和StretchDIBitsStretchBlt:从源矩形中复制一个位图到目标矩形,必要时按目标设备设置的模式进行图像的拉伸或压缩,如果目标设备是窗口DC,则意味着在窗口绘制位图,大致的使用代码如下:1voidDrawImage(HDChdc,HBITMAPhbm,constRECTtarget_rect)2{3HDChdcMemory=::CreateCom…

大家好,又见面了,我是你们的朋友全栈君。

StretchBlt:从源矩形中复制一个位图到目标矩形,必要时按目标设备设置的模式进行图像的拉伸或压缩,如果目标设备是窗口DC,则意味着在窗口绘制位图,大致的使用代码如下:

 1 void DrawImage(HDC hdc, HBITMAP hbm, const RECT target_rect)
 2 {
 3     HDC hdcMemory = ::CreateCompatibleDC(hdc);
 4     HBITMAP old_bmp = (HBITMAP)::SelectObject(hdcMemory, hbm);
 5 
 6     BITMAP bm = { 0 };
 7     ::GetObject(hbm, sizeof(bm), &bm);
 8 
 9     ::StretchBlt(
10         hdc,                              // Target device HDC
11         target_rect.left,                   // X sink position
12         target_rect.top,                    // Y sink position
13         target_rect.right - target_rect.left,    // Destination width
14         target_rect.bottom - target_rect.top,    // Destination height
15         hdcMemory,                               // Source device HDC
16         0,                                // X source position
17         0,                               // Y source position
18         bm.bmWidth,                        // Source width
19         bm.bmHeight,                        // Source height
20         SRCCOPY);                                // Simple copy
21 
22     ::SelectObject(hdcMemory, old_bmp);
23     ::DeleteObject(hdcMemory);
24 }

 StretchDIBits:该函数将DIB(设备无关位图)中矩形区域内像素使用的颜色数据拷贝到指定的目标矩形中,如果目标设备是窗口DC,同样意味着在窗口绘制位图,大致的使用代码如下:

 1 void DrawImage(HDC hdc, LPBITMAPINFOHEADER lpbi, void* bits, const RECT target_rect)
 2 {
 3     ::StretchDIBits(
 4         hdc,                                    // Target device HDC
 5         target_rect.left,                       // X sink position
 6         target_rect.top,                        // Y sink position
 7         target_rect.right - target_rect.left,   // Destination width
 8         target_rect.bottom - target_rect.top,   // Destination height
 9         0,                                      // X source position
10         0,                                      // Adjusted Y source position
11         lpbi->biWidth,                       // Source width
12         abs(lpbi->biHeight),                 // Source height
13         bits,                                   // Image data
14         (LPBITMAPINFO)lpbi,                     // DIB header
15         DIB_RGB_COLORS,                         // Type of palette
16         SRCCOPY);                               // Simple image copy 
18 }

简单的讲,StretchBlt操作的是设备相关位图是HBITMAP句柄,StretchDIBits操作的是设备无关位图是内存中的RGB数据。

DirectShow示例代码中的CDrawImage类提供了FastRender和SlowRender两个函数用于渲染视频图像,FastRender用的StretchBlt,SlowRender用的StretchDIBits,其中SlowRender的注释是这样写的:

1 // This is called when there is a sample ready to be drawn, unfortunately the
2 // output pin was being rotten and didn't choose our super excellent shared
3 // memory DIB allocator so we have to do this slow render using boring old GDI
4 // SetDIBitsToDevice and StretchDIBits. The down side of using these GDI
5 // functions is that the image data has to be copied across from our address
6 // space into theirs before going to the screen (although in reality the cost
7 // is small because all they do is to map the buffer into their address space)

也就是说StretchDIBits比StretchBlt多消耗了从内存地址空间拷贝图像数据到GDI地址空间的时间。实际测试结果在XP和Win7系统下两者效率几乎没有区别,所以可以放心大胆的使用StretchDIBits,毕竟内存数据处理起来要方便的多。

 

 

 

 

 

转载于:https://www.cnblogs.com/xrunning/p/3647046.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148100.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月15日 下午3:16
下一篇 2022年6月15日 下午3:16


相关推荐

  • Cursor集成终端怎么用? Cursor集成终端详细图文使用指南

    Cursor集成终端怎么用? Cursor集成终端详细图文使用指南

    2026年3月16日
    5
  • 4.pycharm添加第三方库[通俗易懂]

    4.pycharm添加第三方库[通俗易懂]网络爬虫的第一步就是根据URL,获取网页的HTML信息。在Python3中,可以使用urllib.request和requests进行网页爬取。(1)准备所需库我们需要准备一款名为BeautifulSoup(网页解析)的开源库,用于对下载的网页进行解析,我们是用的是PyCharm编译环境所以可以直接下载该开源库。urllib库是python内置的,无需我们额外安装,只要安装了Python就可以使用这个库。requests库是第三方库,需要我们自己安装。第三方库安装步骤如下:选择File-&g

    2022年8月29日
    7
  • 中缀表达式转后缀表达式方法_后缀表达式怎么求值

    中缀表达式转后缀表达式方法_后缀表达式怎么求值前言数据结构与算法中经常遇到中缀表达式转前缀表达式的题目,网上的教程大都很不直观,自己学的时候,也走了很多弯路,现在把一个简单易懂的算法教程分享出来。中缀转后缀举个例子,一个式子:(5+20+1∗3)/14(5+20+1*3)/14(5+20+1∗3)/14如何把该式子转换成后缀表达式呢?其实就是分三步:1、按运算符优先级对所有运算符和它的运算数加括号,(原本的括号不用加)2、把运算…

    2025年7月22日
    1
  • python读取excel单元格内容_python如何读取文件夹下的所有文件

    python读取excel单元格内容_python如何读取文件夹下的所有文件1.使用python内建的open()方法读取文本相对路径:example/ex2.txt,文件内容如下所示:测试内容,路径和内容,大家可根据自己心情设置。使用open()方法读取:print(‘—-使用python自带的open()读取文件—–‘)path=r’example/ex2.txt’frame=open(path)print(frame.readlines())此时,执行结果报错如下:我猜测open()方法的…

    2022年10月2日
    4
  • C/C++ 命令解析:getopt 方法详解和使用示例

    C/C++ 命令解析:getopt 方法详解和使用示例一、简介getopt()方法是用来分析命令行参数的,该方法由Unix标准库提供,包含在<unistd.h>头文件中。 二、定义intgetopt(intargc,char*constargv[],constchar*optstring);externchar*optarg;externintoptind,opterr,o…

    2022年4月29日
    43
  • tp3,nginx配置支持pathinfo

    tp3,nginx配置支持pathinfoNginx 默认是不支持 PATHINFO 的第一步 修改 server 块 server listen80 server namewww domain comdomain com error page404 404 html error page50050250 50x html 这个 locat

    2025年9月16日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号