Drawing speed
-
Hi everybody. I trying to draw an overlay over a CDib bitmap:
///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
CSize sizeDoc = pDoc->GetDib()->GetDimensions();
int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
CSize sizeLog(cx, cy);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
CDC MemDC;
MemDC.CreateCompatibleDC(pDC); // create memory device context
CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
pDC->SelectObject(pOldBitmap); // select old bitmap
///*/////////////////////////////////////////////////////////////////////////////////////and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.
-
Hi everybody. I trying to draw an overlay over a CDib bitmap:
///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
CSize sizeDoc = pDoc->GetDib()->GetDimensions();
int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
CSize sizeLog(cx, cy);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
CDC MemDC;
MemDC.CreateCompatibleDC(pDC); // create memory device context
CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
pDC->SelectObject(pOldBitmap); // select old bitmap
///*/////////////////////////////////////////////////////////////////////////////////////and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.
-
The problem is with this line:
pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog);
You need to replace it with another BitBlt that will copy stuff to MemDC
-
Hi everybody. I trying to draw an overlay over a CDib bitmap:
///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
CSize sizeDoc = pDoc->GetDib()->GetDimensions();
int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
CSize sizeLog(cx, cy);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
CDC MemDC;
MemDC.CreateCompatibleDC(pDC); // create memory device context
CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
pDC->SelectObject(pOldBitmap); // select old bitmap
///*/////////////////////////////////////////////////////////////////////////////////////and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.
Google it for "Double Buffering"
-
Google it for "Double Buffering"
-
Thank you for your interest. But I don't know how can I put content of CDib (CMyDoc::m_dib) into MemDC with CDC::BitBlt(...). Can you show me a little sample here ? Will be very helpfull to me ... You can test the behaviuour from above post link.
You need to create DIB (I think you have already already created it based on the code), then you need to copy the bitmap bits to the DIB's buffer using this code:
HBITMAP hOldBitmap = NULL; if (m\_hBitmap == NULL) { m\_hBitmap = CreateDIBSection(hDC, (BITMAPINFO\*)m\_hDib, DIB\_RGB\_COLORS, &m\_lpBits, NULL, 0); if (m\_hBitmap == NULL) return; if (m\_lpBits == NULL) { ::DeleteObject(m\_hBitmap); m\_hBitmap = NULL; return; } // if } // if memcpy(m\_lpBits, ((LPBYTE)m\_hDib + \*(LPDWORD)m\_hDib + GetPaletteSize()), m\_bi.biSizeImage); if (m\_hMemDC == NULL) { m\_hMemDC = CreateCompatibleDC(hDC); if (m\_hMemDC == NULL) return; } // if hOldBitmap = (HBITMAP)SelectObject(m\_hMemDC, m\_hBitmap); BitBlt(hDC, dwX, dwY, m\_bi.biWidth, m\_bi.biHeight, m\_hMemDC, 0, 0, SRCCOPY); SelectObject(m\_hMemDC, hOldBitmap);
The main part here is the memcpy. I have copied it from my program. If you can't understand what the "m_" variables are, let me know and I will explain
-
You need to create DIB (I think you have already already created it based on the code), then you need to copy the bitmap bits to the DIB's buffer using this code:
HBITMAP hOldBitmap = NULL; if (m\_hBitmap == NULL) { m\_hBitmap = CreateDIBSection(hDC, (BITMAPINFO\*)m\_hDib, DIB\_RGB\_COLORS, &m\_lpBits, NULL, 0); if (m\_hBitmap == NULL) return; if (m\_lpBits == NULL) { ::DeleteObject(m\_hBitmap); m\_hBitmap = NULL; return; } // if } // if memcpy(m\_lpBits, ((LPBYTE)m\_hDib + \*(LPDWORD)m\_hDib + GetPaletteSize()), m\_bi.biSizeImage); if (m\_hMemDC == NULL) { m\_hMemDC = CreateCompatibleDC(hDC); if (m\_hMemDC == NULL) return; } // if hOldBitmap = (HBITMAP)SelectObject(m\_hMemDC, m\_hBitmap); BitBlt(hDC, dwX, dwY, m\_bi.biWidth, m\_bi.biHeight, m\_hMemDC, 0, 0, SRCCOPY); SelectObject(m\_hMemDC, hOldBitmap);
The main part here is the memcpy. I have copied it from my program. If you can't understand what the "m_" variables are, let me know and I will explain
-
Google it for "Double Buffering"