思路是首先生成HBITMAP对象,再显示。
在生成HBITMAP对象的时候,主要用到了这个函数
CreateDIBitmap function
Applies to: desktop apps only
The CreateDIBitmap function creates a compatible bitmap (DDB Device Dependent Bitmap) from a DIB (Device Independent Bitmap) and, optionally, sets the bitmap bits.
Syntax
HBITMAP CreateDIBitmap( __in HDC hdc, __in const BITMAPINFOHEADER *lpbmih, __in DWORD fdwInit, __in const VOID *lpbInit, __in const BITMAPINFO *lpbmi, __in UINT fuUsage );
Parameters
-
hdc [in]
-
A handle to a device context.
lpbmih [in]
-
A pointer to a bitmap information header structure, BITMAPV5HEADER.
If fdwInit is CBM_INIT, the function uses the bitmap information header structure to obtain the desired width and height of the bitmap as well as other information. Note that a positive value for the height indicates a bottom-up DIB while a negative value for the height indicates a top-down DIB. Calling CreateDIBitmap with fdwInit as CBM_INIT is equivalent to calling the CreateCompatibleBitmap function to create a DDB in the format of the device and then calling the SetDIBits function to translate the DIB bits to the DDB.
fdwInit [in]
-
Specifies how the system initializes the bitmap bits. The following value is defined.
Value Meaning -
CBM_INIT
If this flag is set, the system uses the data pointed to by the lpbInit and lpbmi parameters to initialize the bitmap bits.
If this flag is clear, the data pointed to by those parameters is not used.
If fdwInit is zero, the system does not initialize the bitmap bits.
lpbInit [in]
-
A pointer to an array of bytes containing the initial bitmap data. The format of the data depends on the biBitCount member of the BITMAPINFO structure to which the lpbmi parameter points.
lpbmi [in]
-
A pointer to a BITMAPINFO structure that describes the dimensions and color format of the array pointed to by the lpbInit parameter.
fuUsage [in]
-
Specifies whether the bmiColors member of the BITMAPINFO structure was initialized and, if so, whether bmiColors contains explicit red, green, blue (RGB) values or palette indexes. The fuUsage parameter must be one of the following values.
Value Meaning -
DIB_PAL_COLORS
A color table is provided and consists of an array of 16-bit indexes into the logical palette of the device context into which the bitmap is to be selected.
-
DIB_RGB_COLORS
A color table is provided and contains literal RGB values.
Return value
If the function succeeds, the return value is a handle to the compatible bitmap.
If the function fails, the return value is NULL.
HDC hdc = ::GetDC(m_hWnd);
HBITMAP hBitmap=NULL;
LPSTR hDib=NULL;
LPSTR PicBuff=NULL;
PicBuff=buffer;
LPVOID pDibBit=NULL;
BITMAPFILEHEADER bmpHead;//位图头结构
DWORD LenBmpHead;
LenBmpHead=sizeof(BITMAPFILEHEADER);
strncpy((LPSTR)&bmpHead,PicBuff,LenBmpHead);
hDib=PicBuff+LenBmpHead;//hDib=PicBuff
BITMAPINFOHEADER &BitmapInfoHead=*(LPBITMAPINFOHEADER)hDib;
BITMAPINFO &BitmapInfo=*(LPBITMAPINFO)hDib;
pDibBit=PicBuff+((BITMAPFILEHEADER*)PicBuff)->bfOffBits;
hBitmap=CreateDIBitmap(hdc,&BitmapInfoHead,CBM_INIT,pDibBit,&BitmapInfo,DIB_RGB_COLORS);
生成HBITMAP后就好说了。
然后用StretchBit 函数显示到客户区。
StretchBlt function
Applies to: desktop apps only
The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.
Syntax
BOOL StretchBlt( __in HDC hdcDest, __in int nXOriginDest, __in int nYOriginDest, __in int nWidthDest, __in int nHeightDest, __in HDC hdcSrc, __in int nXOriginSrc, __in int nYOriginSrc, __in int nWidthSrc, __in int nHeightSrc, __in DWORD dwRop );
Parameters
-
hdcDest [in]
-
A handle to the destination device context.
nXOriginDest [in]
-
The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nYOriginDest [in]
-
The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nWidthDest [in]
-
The width, in logical units, of the destination rectangle.
nHeightDest [in]
-
The height, in logical units, of the destination rectangle.
hdcSrc [in]
-
A handle to the source device context.
nXOriginSrc [in]
-
The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
nYOriginSrc [in]
-
The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
nWidthSrc [in]
-
The width, in logical units, of the source rectangle.
nHeightSrc [in]
-
The height, in logical units, of the source rectangle.
dwRop [in]
-
The raster operation to be performed. Raster operation codes define how the system combines colors in output operations that involve a brush, a source bitmap, and a destination bitmap.
See BitBlt for a list of common raster operation codes (ROPs). Note that the CAPTUREBLT ROP generally cannot be used for printing device contexts.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
HDC MemDC1 = CreateCompatibleDC(hdc);
HGDIOBJ hOldBMP1 = ::SelectObject(MemDC1,hBitmap);
::StretchBlt(hdc,0,0,640,480,MemDC1,0,0,640,480,SRCCOPY);
::SelectObject(MemDC1,hOldBMP1);
::DeleteObject(MemDC1);
DeleteObject((HGDIOBJ)hBitmap);
最后别忘了要删除bmp图占的空间。如
::DeleteObject(MemDC1);
DeleteObject((HGDIOBJ)hBitmap);
小程序可能看不来,但是如果是采集卡上输出的数据,不用几十秒,你的内存就让它给吃完了。