Draw bitmap x-times to screen

Hello,

I am drawing the same resource bitmap several times at different position to the screen. This is done by calling the 'BitBlt()' function in a for-loop. I have discovered some performance problem. Does anybody know how I could improve the performance?
I have written the following code:

int CAnalysView::ShowData(CDC *pDC , CRect *pRect, bool bFromDocument )
{
CPhaseAnalysDoc *pDoc = GetDocument();

ASSERT( pDoc );

// get information about the maximum values that x and y can take
int xMin;
int xSize;
double yMin;
double ySize;


// select the palette into the device context
CPalette* paletteOld = NULL;
paletteOld = pDC->SelectPalette( m_WFPalette.GetPalette(), TRUE ); // In background
ASSERT( paletteOld );
int numentries = pDC->RealizePalette();

int nCnt = 0;
nCnt = pDoc -> GetCount();

int nxMidle = int( double(m_DrawRect.Width())/2 + 0.5 );
int nyMidle = int( double(m_DrawRect.Height())/2 + 0.5 );
int nRadius = min( m_DrawRect.Width()/2, m_DrawRect.Height()/2 - 10 );

CPhaseAnalysDoc::CComplexEntry *pcmpl;

CPoint pt;
{
int nI, nQ;
CPen penSolid( PS_SOLID, 1, m_ColorPen );
CDCSelect select( pDC, &penSolid );
CBrush brush( m_ColorPen );
CDCSelect select2( pDC, &brush );
// loading a bitmap to reduce the cpu-load
CBitmap bitmap;
bitmap.LoadBitmap(m_nIDBitmap);
// Create a Memory DC and Select the BMP to it
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dcMemory.SelectObject(&bitmap);

// get the size of the bitmap
BITMAP bm;
bitmap.GetBitmap( &bm );
CSize size( bm.bmWidth, bm.bmHeight );
pDC->DPtoLP( &size ); // convert device units into logical units.
int nWidth = size.cx;
int nHeight = size.cy;
int nOffsetX = nWidth / 2;
int nOffsetY = nHeight / 2;

// draw the bitmap to the different postion.
for( int t = 0; t < nCnt; t++ )
{
pcmpl = pDoc->Get( t );

if( pcmpl )
{
nI = pcmpl->nI;
nQ = pcmpl->nQ;

pt.x = m_DrawRect.left + nxMidle + nI * nRadius / xMax;
pt.y = m_DrawRect.top + nyMidle - nQ * nRadius / yMax;

// drawing a bitmap ( draw the pixels from the memoryDC to the screen )
pDC->BitBlt(pt.x - nOffsetX, pt.y - nOffsetY, nWidth, nHeight, &dcMemory, 0, 0, SRCCOPY);
}
}
dcMemory.SelectObject( pOldBitmap );
}

return 0; // ok
}

thanks for your help.
best regards.
rgwerder
[2750 byte] By [rgwerder] at [2007-11-19 19:43:59]
# 1 Re: Draw bitmap x-times to screen
You could try memory buffering. Copy the DC passed in into a memory DC; bitblt the birmap to the memory DC; then copy the memory DC back to the DC passed in.

Or, along similar lines, you could probably create an in-memory DC that you copy your bitmap to; then do only one bitblt to the main DC with the appropriate ROP set to only copy your bitmaps over (like masking).

Viggy
MrViggy at 2007-11-10 3:49:34 >
# 2 Re: Draw bitmap x-times to screen
Thanks for your answer, The device context passed to my 'ShowData()' method is already an Memory DC. I already draw the same bitmap several time to a Memory DC before it is drawn to the screen.
CClientDC dc(this);

CDC dcMemory;
dcMemory.CreateCompatibleDC( dc ) );
// create a bitmap for the new device context
CBitmap Bitmap;
Bitmap->CreateCompatibleBitmap( dc, m_memRect.Width(), m_memRect.Height() ) );
// select the new bitmap into the device context
m_pOldBitmap = SelectObject( &Bitmap );

// draw the same bitmap several time.
ShowData( dcMemory, pr, !m_bPauseGraphics );

pDC->BitBlt( pr->left,
pr->top,
pr->Width() ,
pr->Height(),
dcMemory,
pr->left,
pr->top,
SRCCOPY );

Do you have any other idea to improve the performance? Thank you.
Best regards,
rgwerder
rgwerder at 2007-11-10 3:50:32 >