Click to See Complete Forum and Search --> : c++


June 23rd, 1999, 02:55 PM
Im just wondering if anyone can tell me what API or function I would use to load a bitmap into a DC using Borland C++ 4.52, I don't own visual c++ but I figured someone would know.. thanks.
-Dave

Radracr
June 23rd, 1999, 03:38 PM
This is all Win32 API This code loads a bitmap from a file into a HBITMAP then into a DC and then Stretches it to fit the window of a control in your dialog that you are displaying it to.

Hope it helps.


HDC hPhotoWndDC;
HDC hCompatDC;
HWND hPhotoWnd;

m_hCurrentBmp = (HBITMAP)::LoadImageW(NULL, bstrFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (m_hCurrentBmp != NULL)
{
hPhotoWnd = ::GetDlgItem(hWnd, uCtrl);
if (hPhotoWnd != NULL)
{
hPhotoWndDC = ::GetDC(hPhotoWnd);
if (hPhotoWndDC != NULL)
{
hCompatDC = ::CreateCompatibleDC(hPhotoWndDC);
if (hCompatDC != NULL)
{
RECT rect;
::GetClientRect(hPhotoWnd, &rect);
HBITMAP oldBitmap = (HBITMAP)::SelectObject(hCompatDC, m_hCurrentBmp);
BITMAP bm;
::GetObject( m_hCurrentBmp, sizeof( bm ), &bm );
::StretchBlt(hPhotoWndDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top, hCompatDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
::SelectObject(hCompatDC, oldBitmap);
::ReleaseDC(hPhotoWnd, hCompatDC);
hr = S_OK;
}
::ReleaseDC(hPhotoWnd, hPhotoWndDC);
}
}
}

Rauli Ikonen
June 23rd, 1999, 03:46 PM
// This should give you a clue how to do it
bool BitmapToDC(char *szImage)
{
// Try loading the image.
HBITMAP hbm = (HBITMAP)LoadImage(NULL, szImage, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

if (hbm == NULL)
return false;

HDC hdcImage = CreateCompatibleDC(NULL);
SelectObject(hdcImage, hbm);

// if you need to draw the image on some window use GetDC and BitBlt (and ReleaseDC)

if (hdc)
DeleteDC(hdc);
if (hbm)
DeleteObject(hbm);
return true;
}