Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Load JPEG from file

68 views
Skip to first unread message

Chris

unread,
Feb 13, 2003, 5:37:15 PM2/13/03
to
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?

Regards
Chris

Jackson Yee

unread,
Feb 13, 2003, 7:39:47 PM2/13/03
to
"Chris" <dh...@softhome.net> wrote in message
news:5e7o4vs44lqfd4ogu...@4ax.com...

> 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?

If you want it to be compatible with all Windows platforms, then it's pretty
hard to do it simply using the API. libjpeg will give you much more
flexibility and options in terms of formatting and operating.

Feel free to look at the source for PaintLib if you're a C++ user though:

http://www.paintlib.de/paintlib/

--
Regards,
Jackson Yee
jy...@vt.edu


Ted Ferenc

unread,
Feb 14, 2003, 4:15:15 AM2/14/03
to
I use CxImage freeware with source code see:-

http://www.codeproject.com/bitmap/cximage.asp

very simple to use, but if you want to learn the basics then it is probably
not what you want.

--

Ted Ferenc
This address used is maintained only for newsgroup posting.
Mail sent there may not be read by me for several days.


"Chris" <dh...@softhome.net> wrote in message
news:5e7o4vs44lqfd4ogu...@4ax.com...


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.454 / Virus Database: 253 - Release Date: 10/02/03


Lucian Wischik

unread,
Feb 14, 2003, 6:16:52 AM2/14/03
to
Chris <dh...@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

Max

unread,
Feb 15, 2003, 6:11:36 AM2/15/03
to
Use GDI + Win 98 and above


"Chris" <dh...@softhome.net> wrote in message
news:5e7o4vs44lqfd4ogu...@4ax.com...

0 new messages