Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing bitmaps without memory leaks. HELP.

Status
Not open for further replies.

qedusa

Programmer
Jan 12, 2002
43
US
I'm trying to draw a bitmap image repeatedly. However, it crashes when it's draw the same image about 1,000 times - I suspect due to a memory leak. I need someone to tell me the best way to display a bitmap and then destroy the associated memory immediately afterwards. Any sample code anyone?
 
You should override OnDraw() Function in Your CWnd-derived class.
An example for a Bitmap with Handle hBitmap (created with CreateBitmap(), CreateDIBitmap() etc.):

HBITMAP AnyFunktion()
{
...
HBITMAP hBitmap = CreateDIBitmap(...); //Get Your Bitmap Handle
...
return hBitmap;
}

void CMyView::OnDraw(CDC* pDC)
{
HDC hdcMem = CreateCompatibleDC(pDC->m_hDC);
if(hdcMem) {
RECT rct;
GetClientRect(&rct);
HGDIOBJ hobj = SelectObject(hdcMem,hBitmap);
if(hobj) {
CPoint pnt = GetScrollPosition( ); //If the Bitmap is biggeras View
BitBlt(pDC->m_hDC, pnt.x, pnt.y, pnt.x + rct.right, pnt.y + rct.bottom, hdcMem, pnt.x, pnt.y, SRCCOPY);
}
DeleteDC(hdcMem);
}
}

Do not forget DeleteObject(hBitmap) after You do not need it more.
Do not create or delete Bitmap in OnDraw() - it makes Your Program slow.
 
Thanks tchouch, I realized that the problem wasn't my bitmap drawing code but something to do with my [shape] drawing. I'm trying to draw a shape and then a bitmap image on top. When I started to have problems repeating the process I naturally blamed the bitmap!!
However, I have found your tip (example) useful anyway for speeding up the drawing process!!
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top