1

I am drawing bitmap images on graphics object using DrawImage method But the Images are in large number so it is taking too much time for Drawing. I have read in this forum that using StretchDIBits takes less time for Drawing. I am scaling the image by calling Drawimage but i want any other efficent method. I have a Vector of Bitmap* & i want to draw each Bitmap on graphics.

HDC orghDC = graphics.GetHDC();
CDC *dc = CDC::FromHandle(orghDC);

m_vImgFrames is image vector containg Bitmap*. I have taken HBITMAP from Bitmap*.

HBITMAP hBitmap;
m_vImgFrames[0]->GetHBITMAP(Color(255,0,0),&hBitmap);

Using this HBITMAP i want to draw on orghDC & finally on graphics. So I want to know how StretchDIBits can be used for scaling the Bitmap and finally draw on Graphics Object.

I am new to this forum.Any ideas or code can be helpful

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
VideoDev
  • 111
  • 2
  • 7

2 Answers2

1

Instead of using StretchDIBits, why not use the GDI+ API directly to scale the bitmap?:

CRect rc( 0, 0, 20, 30 );

graphics.DrawImage( (Image*)m_vImgFrames[0], 
    rc.left, rc.top, rc.Width(), rc.Height() );
Alan
  • 13,510
  • 9
  • 44
  • 50
  • Thanks for your reply.I have already tried using DrawImage but it is very slow.I have found the way how to use StretchDIBits & i got very good performance. – VideoDev Jul 10 '09 at 10:18
  • i was going to comment on your question, but VideoDev beat me to it. You don't want to use DrawImage because it is very slow. – Ian Boyd Jan 16 '10 at 14:27
0

To use StretchDIBits with Gdiplus::Bitmap you could do the following:

// get HBITMAP
HBITMAP hBitmap;
m_vImgFrames[0]->GetHBITMAP( Gdiplus::Color(), &hBitmap );
// get bits and additional info
BITMAP bmp = {};
::GetObject( hBitmap, sizeof(bmp), &bmp );
// prepare BITMAPINFO
BITMAPINFO bminfo = {};
bminfo.bmiHeader.biSize = sizeof( BITMAPINFO );
bminfo.bmiHeader.biWidth = bmp.bmWidth;
bminfo.bmiHeader.biHeight = bmp.bmHeight;
bminfo.bmiHeader.biBitCount = bmp.bmBitsPixel;
bminfo.bmiHeader.biCompression = BI_RGB;
bminfo.bmiHeader.biPlanes = bmp.bmPlanes;
bminfo.bmiHeader.biSizeImage = bmp.bmWidthBytes*bmp.bmHeight*4; // 4 stands for 32bpp
// select stretch mode
::SetStretchBltMode( HALFTONE );
// draw
::StretchDIBits( hDC, 0, 0, new_cx, new_cy, 0, 0,
  m_vImgFrames[0]->GetWidth(), m_vImgFrames[0]->GetHeight(), 
  bmp.bmBits, &bminfo, DIB_RGB_COLORS, SRCCOPY );

But this doesn't looks much faster on my machine than simple Graphics::DrawImage.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212