從緩衝區中載入BMP位元影像並返回控制代碼

來源:互聯網
上載者:User

//----------------------------------------------------------------------------<br />// FUNCTION: LoadBitmapFromBuffer<br />// DESC: 從緩衝區中載入位元影像並返回控制代碼<br />//----------------------------------------------------------------------------<br />HBITMAP LoadBitmapFromBuffer(LPBYTE pBuffer, DWORD cbSize)<br />{<br />BITMAPFILEHEADER *pbmfHeader = LPBITMAPFILEHEADER(pBuffer);// File type should be 'BM'<br />BITMAPINFOHEADER *pbmiHeader = LPBITMAPINFOHEADER(pBuffer + sizeof(BITMAPFILEHEADER));<br />BITMAPINFO *pbmInfo = LPBITMAPINFO (pBuffer + sizeof(BITMAPFILEHEADER));<br />// If bmiHeader.biClrUsed is zero we have to infer the number of colors from the number of bits used to specify it.<br />int nColors = pbmiHeader->biClrUsed ? pbmiHeader->biClrUsed : 1 << pbmiHeader->biBitCount;<br />if (pbmfHeader->bfType != (WORD('M' << 8) | 'B'))<br />{<br />return NULL;<br />}<br />LPVOID lpDIBBits;<br />if( pbmInfo->bmiHeader.biBitCount > 8 )<br />{<br />lpDIBBits = LPVOID(LPDWORD(pbmInfo->bmiColors + pbmInfo->bmiHeader.biClrUsed) + ((pbmInfo->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));<br />}<br />else<br />{<br />lpDIBBits = LPVOID(pbmInfo->bmiColors + nColors);<br />}</p><p>HDC hdc = GetDC(NULL); // Create the palette<br />HPALETTE hPal, hOldPal;<br />if( nColors <= 256 )<br />{<br />UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);<br />LOGPALETTE *pLP = (LOGPALETTE *) malloc(nSize);<br />pLP->palVersion = 0x300;<br />pLP->palNumEntries = nColors;<br />for( int i = 0; i < nColors; i++ )<br />{<br />pLP->palPalEntry[i].peRed = pbmInfo->bmiColors[i].rgbRed;<br />pLP->palPalEntry[i].peGreen = pbmInfo->bmiColors[i].rgbGreen;<br />pLP->palPalEntry[i].peBlue = pbmInfo->bmiColors[i].rgbBlue;<br />pLP->palPalEntry[i].peFlags = 0;<br />}<br />hPal = CreatePalette(pLP);<br />free(pLP);<br />hOldPal=(HPALETTE)SelectObject(hdc, hPal);<br />RealizePalette(hdc);<br />}</p><p>HBITMAP hBmp = CreateDIBitmap( hdc, // handle to device context<br /> pbmiHeader, // pointer to bitmap size and format data<br />CBM_INIT, // initialization flag<br />lpDIBBits, // pointer to initialization data<br />pbmInfo, // pointer to bitmap color-format data<br />DIB_RGB_COLORS); // color-data usage<br />if( hPal )<br />{<br />SelectObject(hdc, hOldPal);<br />DeleteObject(hPal);<br />}<br />ReleaseDC(NULL, hdc);<br />return hBmp;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.