CImage修改图片尺寸


bool ResizePicture(CString strSource, CString strTarget)
{
    int WIDTH = 70;
    int HEIGHT = 70;
    CImage oldimg;
    CImage newimg;
    oldimg.Load(strSource);
    if (oldimg.IsNull())
        return false;
    int nWidth = 0;
    int nHeight = 0;

    nWidth = oldimg.GetWidth();
    nHeight = oldimg.GetHeight();

    if (nWidth > WIDTH || nHeight > HEIGHT)
    {
        double dRatio = nWidth * 1.0 / nHeight;
        if (nWidth > nHeight)
        {
            nWidth = WIDTH;
            nHeight = (int)(nWidth / dRatio);
        }
        else
        {
            nHeight = HEIGHT;
            nWidth = (int)(nHeight * dRatio);
        }
    }

    if (!newimg.CreateEx(nWidth, nHeight, 24, BI_RGB))
    {
        oldimg.Destroy();
        return false;
    }

    int nPreMode = ::SetStretchBltMode(newimg.GetDC(), HALFTONE);
    newimg.ReleaseDC();
    oldimg.Draw(newimg.GetDC(), 0, 0, nWidth, nHeight, 0, 0, oldimg.GetWidth(),                       oldimg.GetHeight());
    newimg.ReleaseDC();
    ::SetBrushOrgEx(newimg.GetDC(), 0, 0, NULL);
    newimg.ReleaseDC();
    ::SetStretchBltMode(newimg.GetDC(), nPreMode);
    newimg.ReleaseDC();

    newimg.Save(strTarget);
    newimg.Destroy();
    oldimg.Destroy();

    return true;
}

猜你喜欢

转载自blog.csdn.net/sichuanpb/article/details/82619733
今日推荐