StretchBlt和StretchDIBits
StretchBlt:從源矩形中復制一個位圖到目標矩形,必要時按目標設(shè)備設(shè)置的模式進行圖像的拉伸或壓縮,如果目標設(shè)備是窗口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:該函數(shù)將DIB(設(shè)備無關(guān)位圖)中矩形區(qū)域內(nèi)像素使用的顏色數(shù)據(jù)拷貝到指定的目標矩形中,如果目標設(shè)備是窗口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操作的是設(shè)備相關(guān)位圖是HBITMAP句柄,StretchDIBits操作的是設(shè)備無關(guān)位圖是內(nèi)存中的RGB數(shù)據(jù)。
DirectShow示例代碼中的CDrawImage類提供了FastRender和SlowRender兩個函數(shù)用于渲染視頻圖像,F(xiàn)astRender用的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多消耗了從內(nèi)存地址空間拷貝圖像數(shù)據(jù)到GDI地址空間的時間。實際測試結(jié)果在XP和Win7系統(tǒng)下兩者效率幾乎沒有區(qū)別,所以可以放心大膽的使用StretchDIBits,畢竟內(nèi)存數(shù)據(jù)處理起來要方便的多。
轉(zhuǎn)載于:https://www.cnblogs.com/xrunning/p/3647046.html
總結(jié)
以上是生活随笔為你收集整理的StretchBlt和StretchDIBits的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gateway response ann
- 下一篇: Fiori elements执行过程解析