部分StretchDIBits时图像抖动

时间:2009-07-09 08:48:35

标签: integer shake stretchdibits approximate

我正在使用C ++ GDI,StretchDIBits在DC上绘制图像。

因为原始图像很大,所以需要高质量。 我使用HAFTONE模式,在DC上绘制整个图像(缩放图像)似乎耗费时间。

所以我决定使用StretchDIBits进行部分绘制。 但StretchDIBits存在严重问题。

我只能将整数中的rect绘制为整数区域(宽度和高度,x,y的左上角都是整数)

    ::SetStretchBltMode(hdc, HALFTONE);
    ::StretchDIBits(hdc,
        realDrawRect.left, 
        realDrawRect.top, 
        realDrawRect.right - realDrawRect.left, 
        realDrawRect.bottom - realDrawRect.top,
        left, 
        top, 
        width, 
        height,
        pImageDIB,
        pImageHead, 
        DIB_RGB_COLORS, SRCCOPY);

如果,图像是21 * 21。 我现在画(5,5,7,7)到DC,在(20,20,60,60),下次我想画(21,20,61,60)。 原始图像上没有对应的位置。所以我只能在DC上绘制一个近似矩形。现在问题发生了,图像在摇晃!!

我对这个问题感到恼火。 我怎么能避免晃动?

2 个答案:

答案 0 :(得分:0)

如果振动来自舍入整数,请尝试将其四舍五入为最接近的整数。请参阅this

否则我建议您切换到更容易使用的GDI +。使用GDI +,您可以绘制具有浮点精度的位图。请参阅文档Graphics::DrawImage

我已经在几个MFC Active-X项目中从GDI切换到GDI +并且它并不难。

答案 1 :(得分:0)

这是我在C ++和C#中使用stretchDIBits时遇到的一个正常问题。为了克服这个问题,我们可以在调用StretchDIBits功能之前先校正图像的宽度和高度:

if (m_currentImage.Width % 2 != 0) // check for odd number
{
     if (((m_currentImage.Width - 1) / 2) % 2 != 0)
           m_currentImage = m_currentImage.Resize(m_currentImage.Width - 3, m_currentImage.Height - 2, Emgu.CV.CvEnum.Inter.Linear);
     else
           m_currentImage = m_currentImage.Resize(m_currentImage.Width - 1, m_currentImage.Height, Emgu.CV.CvEnum.Inter.Linear);
}
else if ((m_currentImage.Width / 2) % 2 != 0) // else check fraction of width is odd number
     m_currentImage = m_currentImage.Resize(m_currentImage.Width - 2, m_currentImage.Height - 1, Emgu.CV.CvEnum.Inter.Linear);
相关问题