在知道位图数据和BITMAPINFO后,可以用SetDIBits()函数创建一位图句柄..然后在内存中准备一个hMemDC,在这上面把两张位图拼接成一块位图;
       代码如下:
码流代码_职场//这个函数在内存中构造一张位图,把两张位图拼接成一张.
码流代码_职场HBITMAP GetBmp()
码流代码_职场{
码流代码_职场    HBITMAP hBmp1, hBmp2;
码流代码_职场    SetDIBits(hBmp1, pStream1, bmpInfo1...); //得到位图1的句柄
码流代码_职场    SetDIBits(hBmp2, pStream2, bmpInfo2...); //得到位图2的句柄
码流代码_职场    //把这两张位图选入内存
码流代码_职场    HDC hDC1, hDC2;
码流代码_职场    SelectObject(hDC1, hBmp1);
码流代码_职场    SelectObject(hDC1, hBmp1);
码流代码_职场
码流代码_职场    //下面在内存中准备HDC ,和HBITMAP,用于拼接位图
码流代码_职场    HDC hMemDC;
码流代码_职场    HBITMAP hMemBmp = CreateCompatibleBitmap();
码流代码_职场    SelectObject(hMemDC, hMemBmp);
码流代码_职场    //下面就可以把这两张位图加载到这张准备好的内在位图中,调用函数BitBlt()
码流代码_职场    BitBlt(hMemDC, .... hDC1,...); //先把第一张位图选入内存
码流代码_职场    BitBlt(hMemDC, .... hDC2,...); //先把第二张位图选入内存
码流代码_职场    return hMemBmp;
码流代码_职场}
码流代码_职场//下面是把一张位图写入文件..代码来自MSDN
码流代码_职场void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,    
码流代码_职场                                    HBITMAP hBMP, HDC hDC)    
码流代码_职场 {    
码流代码_职场         HANDLE hf;                                 // file handle    
码流代码_职场        BITMAPFILEHEADER hdr;             // bitmap file-header    
码流代码_职场        PBITMAPINFOHEADER pbih;         // bitmap info-header    
码流代码_职场        LPBYTE lpBits;                            // memory pointer    
码流代码_职场        DWORD dwTotal;                            // total count of bytes    
码流代码_职场        DWORD cb;                                     // incremental count of bytes    
码流代码_职场        BYTE *hp;                                     // byte pointer    
码流代码_职场        DWORD dwTmp;    
码流代码_职场
码流代码_职场        pbih = (PBITMAPINFOHEADER) pbi;    
码流代码_职场        lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
码流代码_职场
码流代码_职场        if (!lpBits)    
码流代码_职场                 errhandler("GlobalAlloc", hwnd);    
码流代码_职场
码流代码_职场        // Retrieve the color table (RGBQUAD array) and the bits    
码流代码_职场        // (array of palette indices) from the DIB.    
码流代码_职场        if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,    
码流代码_职场                DIB_RGB_COLORS))    
码流代码_职场        {
码流代码_职场                errhandler("GetDIBits", hwnd);    
码流代码_职场        }
码流代码_职场
码流代码_职场        // Create the .BMP file.    
码流代码_职场        hf = CreateFile(pszFile,    
码流代码_职场                                     GENERIC_READ | GENERIC_WRITE,    
码流代码_职场                                     (DWORD) 0,    
码流代码_职场                                        NULL,    
码流代码_职场                                     CREATE_ALWAYS,    
码流代码_职场                                     FILE_ATTRIBUTE_NORMAL,    
码流代码_职场                                     (HANDLE) NULL);    
码流代码_职场        if (hf == INVALID_HANDLE_VALUE)    
码流代码_职场                errhandler("CreateFile", hwnd);    
码流代码_职场        hdr.bfType = 0x4d42;                // 0x42 = "B" 0x4d = "M"    
码流代码_职场        // Compute the size of the entire file.    
码流代码_职场        hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +    
码流代码_职场                                 pbih->biSize + pbih->biClrUsed    
码流代码_职场                                 * sizeof(RGBQUAD) + pbih->biSizeImage);    
码流代码_职场        hdr.bfReserved1 = 0;    
码流代码_职场        hdr.bfReserved2 = 0;    
码流代码_职场
码流代码_职场        // Compute the offset to the array of color indices.    
码流代码_职场        hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +    
码流代码_职场                                        pbih->biSize + pbih->biClrUsed    
码流代码_职场                                        * sizeof (RGBQUAD);    
码流代码_职场
码流代码_职场        // Copy the BITMAPFILEHEADER into the .BMP file.    
码流代码_职场        if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),    
码流代码_职场                (LPDWORD) &dwTmp,    NULL))    
码流代码_职场        {
码流代码_职场             errhandler("WriteFile", hwnd);    
码流代码_职场        }
码流代码_职场
码流代码_职场        // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.    
码流代码_职场        if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)    
码流代码_职场                                    + pbih->biClrUsed * sizeof (RGBQUAD),    
码流代码_职场                                    (LPDWORD) &dwTmp, ( NULL))    
码流代码_职场                errhandler("WriteFile", hwnd);    
码流代码_职场
码流代码_职场        // Copy the array of color indices into the .BMP file.    
码流代码_职场        dwTotal = cb = pbih->biSizeImage;    
码流代码_职场        hp = lpBits;    
码流代码_职场        if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))    
码流代码_职场                     errhandler("WriteFile", hwnd);    
码流代码_职场
码流代码_职场        // Close the .BMP file.    
码流代码_职场         if (!CloseHandle(hf))    
码流代码_职场                     errhandler("CloseHandle", hwnd);    
码流代码_职场
码流代码_职场        // Free memory.    
码流代码_职场        GlobalFree((HGLOBAL)lpBits);
码流代码_职场}
本文出自 “Paul玩嵌入式Linux” 博客,请务必保留此出处http://zyg0227.blog.51cto.com/1043164/314505