在GDI +图形对象上绘制使用StretchDIBits进行缩放的位图

时间:2009-06-07 10:45:05

标签: c++ gdi+ stretchdibits

我使用DrawImage方法在图形对象上绘制位图图像但是图像数量很大,因此绘制时间太长。我在这个论坛中读过,使用StretchDIBits花费的时间更少。    我通过调用Drawimage缩放图像,但我想要任何其他有效的方法。 我有一个位图矢量*&我想在图形上绘制每个位图。

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

m_vImgFrames是包含Bitmap*的图像矢量。我从Bitmap*获取了HBITMAP。

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

使用这个HBITMAP我想在orghDC&上画画最后在图形上。所以我想知道StretchDIBits如何用于缩放Bitmap并最终在Graphics Object上绘制。

我是这个论坛的新手。任何想法或代码都可以提供帮助

2 个答案:

答案 0 :(得分:1)

为什么不直接使用GDI + API来缩放位图,而不是使用StretchDIBits?:

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

graphics.DrawImage( (Image*)m_vImgFrames[0], 
    rc.left, rc.top, rc.Width(), rc.Height() );

答案 1 :(得分:0)

要将StretchDIBitsGdiplus::Bitmap一起使用,您可以执行以下操作:

// 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 );

但是这在我的机器上看起来并不比简单的Graphics::DrawImage快得多。

相关问题