From: Crispen, Bob (bob.crispen_at_boeing.com)
Date: 27 February 2003
Subject: Re: Load JPEG from file
Date: Fri, 14 Feb 2003 12:16:52 +0100
From: Lucian Wischik <lu.nn_at_wischik.com>
Organization: University of Bologna
Newsgroups: comp.os.ms-windows.programmer.win32
Chris <dhira_at_softhome.net> wrote:
>Can someone help me with an example of how I load a JPEG from a file
>and draws it on the DC in pure Windows API?
Look up OleLoadPicture and OleLoadPicturePath. They load JPEGs and are present in all versions of Windows. Well, that's what I read recently, but I haven't yet had time to do it myself. Here's the code sample I picked up:
void LoadDibFromFile(LPCTSTR szFile, CDib *pCDib) {
// open file
HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
// alloc memory based on file size
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
// read file and store in global memory
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
// Create IPicture from image file
LPPICTURE pPicture;
hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID
*)&pPicture);
_ASSERTE(SUCCEEDED(hr) && pPicture);
pstm->Release();
// get the bitmap handle
HBITMAP hBitmap;
if (S_OK != pPicture->get_Handle((OLE_HANDLE FAR *) &hBitmap))
{
TRACE("Couldn't get bitmap handle from picture\n");
ASSERT(FALSE);
}
// create a memory DC for the bitmap
HDC memDC = CreateCompatibleDC(NULL);
HBITMAP oldBitmap;
oldBitmap = (HBITMAP) SelectObject(memDC, hBitmap);
// find the size of the DIB to be created
BITMAPINFO bInfo;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bInfo.bmiHeader.biBitCount = 0;
if (!GetDIBits(memDC, hBitmap, 0, 0, NULL, &bInfo, DIB_RGB_COLORS))
{
DWORD error = GetLastError();
ASSERT(FALSE);
}
int totalSize = PackedDibGetInfoHeaderSize(&bInfo) +
PackedDibGetColorTableSize(&bInfo) +
PackedDibGetBitsSize(&bInfo);
// allocate and create a packed DIB
LPBITMAPINFO packedBitmap = (LPBITMAPINFO) new BYTE[totalSize];
CopyMemory(packedBitmap, &bInfo, sizeof(BITMAPINFO));
if (!GetDIBits(
memDC,
hBitmap,
0,
PackedDibGetHeight(&bInfo),
(void *) (((BYTE *) packedBitmap) +
PackedDibGetInfoHeaderSize(&bInfo) +
PackedDibGetColorTableSize(&bInfo)),
packedBitmap,
DIB_RGB_COLORS))
{
DWORD error = GetLastError();
ASSERT(FALSE);
}
// attach the packed DIB to the CDib object. The object is now responsible
// for deletion (it should do this in the destructor)
pCDib->AttachMemory((LPVOID) packedBitmap, TRUE);
// clean up the memory DC
SelectObject(memDC, oldBitmap);
DeleteDC(memDC);
-- Lucian
This archive was generated by hypermail pre-2.1.8 : 26 April 2003 EDT