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


相关推荐

  • Jupyter notebook/Pycharm调用Anaconda虚拟环境

    Jupyter notebook/Pycharm调用Anaconda虚拟环境文章目录 Jupyternoteb 调用 Anaconda 虚拟环境 Pycharm 调用 Anaconda 虚拟环境 Jupyternoteb 调用 Anaconda 虚拟环境本文章适合已安装好 Anaconda 的 uu 们使用 如果还没有安装好 Anaconda 或者对 Anaconda 不了解的可以看我这篇文章哦 绝对让你恍然大悟首先我们打开 anacondaprom 激活虚拟环境 在当前环境中安装 ipykernelcon y 继续在该环境中安装 nb cond

    2026年3月18日
    1
  • iOS逆向入门实践 — 逆向微信,伪装定位(二)

    iOS逆向入门实践 — 逆向微信,伪装定位(二)1.创建工程还是跟之前的步骤一致,创建工程,然后配置Makefile。注意创建工程时名字只能包含数字跟字母。Makefile上面引入的 FakeWeChatLocationManager.m 文件是主要代码实现的地方,分离出来好模块化。2.Reveal注入为了更好地定位代码,需要分析一下“附近的人”这个界面对应的类名是什么,然后进一步分析

    2022年6月2日
    44
  • LCD Keypad Shield

    LCD Keypad Shieldhttp://wiki.dfrobot.com.cn/index.php/(SKU:DFR0009)LCD_Keypad_Shield(Arduino兼容)简介LCDKeypadShield是一款提供2行16字符液晶显示的Arduino扩展板。扩展了多个按键输入,可供用户作为LCD显示屏的菜单选择按键或者操控按键使用。一个扩展板就能让你与Arduino设备进行互动。我们还扩展Ardui…

    2022年4月30日
    40
  • 史上最全的数字IC后端设计实现培训教程(整理版)

    史上最全的数字IC后端设计实现培训教程(整理版)史上最全的数字IC后端设计实现培训教程(整理版)由于最近比较忙,前几天才把五月份开展活动送的书全部寄出,预计最迟明后天就会送到各位手中,希望各位多多理解!本次活动共送出八本《LowPowerFlow》PhysicalImplementation(BackEnd)纸质书籍,请各位中奖的朋友注意查收!另外本次小编多打印了五六本,有需要的朋友可以按照成本价送出(小编微信ic-backend2018)。鉴于很多小伙伴们经常苦于找各类数字IC后端实现培训教程和培训视频,今天小编特此整理了一份非常全

    2022年7月19日
    27
  • 001-圆周率1万位「建议收藏」

    001-圆周率1万位「建议收藏」3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564

    2025年11月3日
    11
  • SQL的多表查询

    SQL的多表查询

    2021年7月20日
    65

发表回复

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

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