如何將內存中的位圖數據繪制在DC上


假如你定義了一個位圖類,里面包含位圖頭,位圖信息頭,調色板,位圖數據。然后你按照位圖的格式將位圖文件讀入你的類中,現在你知道了位圖的全部信息了。主要信息包含在位圖信息頭里面,數據則在位圖數據緩沖里面。現在的問題是,在Windows下面如何將一張位圖畫出來,而且現在是如何從數據緩存里面繪畫出位圖。
  一般情況,我們都是直接繪制在dc里面,而不是綁定到子控件,讓子控件自己繪畫,比如picture控件之類的,我覺得提供繪制在dc里面的接口更具有廣泛性。

  現在我知道兩種從內存數據繪制彩色位圖的2種方法。第一種麻煩一點,第二種則相當直接。
  方法一:
  第一步,用CreateCompatibleDC創建跟目標dc的兼容性內存dc。
  第二步,用CreateCompatibleBitmap創建跟目標dc的兼容性位圖。
  第三步,用SelectObject將第二步創建的兼容位圖選入第一步創建的兼容dc中。
  第四步,用SetDIBits設置兼容位圖的數據緩沖
  第五步,用BitBlt將數據從兼容內存dc繪制到目標dc。
  第六步,刪除兼容位圖和兼容dc。
  代碼如下,其中buffer代表位圖數據緩沖。

1 HDC hCompatibleDC = CreateCompatibleDC(hDc);
2 HBITMAP hCompatibleBitmap = CreateCompatibleBitmap(hDc, bitmapinfoheader.biWidth,
3 bitmapinfoheader.biHeight);
4 HBITMAP hOldBitmap = (HBITMAP)SelectObject(hCompatibleDC, hCompatibleBitmap);
5 SetDIBits(hDc, hCompatibleBitmap, 0, bitmapinfoheader.biHeight,
6 buffer, (BITMAPINFO*)&bitmapinfoheader, DIB_RGB_COLORS);
7 BitBlt(hDc, nStartX, nStartY, bitmapinfoheader.biWidth, bitmapinfoheader.biHeight,
8 hCompatibleDC, 0, 0, SRCCOPY);
9 SelectObject(hCompatibleDC, hOldBitmap);
10 DeleteObject(hCompatibleDC);
11 DeleteObject(hCompatibleDC);



  方法二:直接調用StretchDIBits繪制位圖
  該函數功能相當強悍,似乎專為從內存數據繪制位圖到dc而生。
  函數原型如下:
   int StretchDIBits(
   HDC hdc,                      // handle to DC
   int XDest,                    // x-coord of destination upper-left corner
   int YDest,                    // y-coord of destination upper-left corner
   int nDestWidth,               // width of destination rectangle
   int nDestHeight,              // height of destination rectangle
   int XSrc,                     // x-coord of source upper-left corner
   int YSrc,                     // y-coord of source upper-left corner
   int nSrcWidth,                // width of source rectangle
   int nSrcHeight,               // height of source rectangle
   CONST VOID *lpBits,           // bitmap bits
   CONST BITMAPINFO *lpBitsInfo, // bitmap data
   UINT iUsage,                  // usage options
   DWORD dwRop                   // raster operation code
   );
  使用也相當簡單,調用

1 StretchDIBits(hDc, nStartX, nStartY, bitmapinfoheader.biWidth,
2 bitmapinfoheader.biHeight, 0, 0, bitmapinfoheader.biWidth,
3 bitmapinfoheader.biHeight, buffer, (BITMAPINFO*)&bitmapinfoheader,
4 DIB_RGB_COLORS, SRCCOPY);

  即可了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM