最新消息:想得多,做的少。一天到晚瞎鸡巴搞。

图片等比例缩放2

Qt 阿虚 605浏览 0评论

.      以前弄过一篇《图片等比例缩放》,不过后来测试图片有些问题。然后重新找了一份代码

//c++
CRect RectSizeWithConstantRatio(CRect* rcScreen, CSize sizePicture)
{
    CRect rect(rcScreen);
    double dWidth = rcScreen->Width();
    double dHeight = rcScreen->Height();
    double dAspectRatio = dWidth / dHeight;
    double dPictureWidth = sizePicture.cx;
    double dPictureHeight = sizePicture.cy;
    double dPictureAspectRatio = dPictureWidth / dPictureHeight;
    //If the aspect ratios are the same then the screen rectangle  
    // will do, otherwise we need to calculate the new rectangle  
    if (dPictureAspectRatio > dAspectRatio)
    {
        int nNewHeight = (int)(dWidth / dPictureWidth*dPictureHeight);
        int nCenteringFactor = (rcScreen->Height() - nNewHeight) / 2;
        rect.SetRect(0,
            nCenteringFactor,
            (int)dWidth,
            nNewHeight + nCenteringFactor);
    }
    else if (dPictureAspectRatio < dAspectRatio) { int nNewWidth = (int)(dHeight / dPictureHeight*dPictureWidth); int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;
        rect.SetRect(nCenteringFactor,
            0,
            nNewWidth + nCenteringFactor,
            (int)(dHeight));
    }
    
    return rect;
}

//Qt
QSize CControlImage::RectSizeWithConstantRatio(QSize* rcScreen, QSize sizePicture)
{
    QSize rect(*rcScreen);
    double dWidth = rcScreen->width();
    double dHeight = rcScreen->height();
    double dAspectRatio = dWidth / dHeight;
    double dPictureWidth = sizePicture.width();
    double dPictureHeight = sizePicture.height();
    double dPictureAspectRatio = dPictureWidth / dPictureHeight;
    //If the aspect ratios are the same then the screen rectangle  
    // will do, otherwise we need to calculate the new rectangle  
    if (dPictureAspectRatio > dAspectRatio)
    {
        int nNewHeight = (int)(dWidth / dPictureWidth*dPictureHeight);
        int nCenteringFactor = (rcScreen->height() - nNewHeight) / 2;
        
        rect.setHeight(nNewHeight + nCenteringFactor);
        rect.setWidth((int)dWidth); 
    }
    else if (dPictureAspectRatio < dAspectRatio) { int nNewWidth = (int)(dHeight / dPictureHeight*dPictureWidth); int nCenteringFactor = (rcScreen->width() - nNewWidth) / 2;

        rect.setHeight((int)(dHeight));
        rect.setWidth(nNewWidth + nCenteringFactor); 
    }
    return rect;
}

转载请注明:虚无 » 图片等比例缩放2

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址