zoukankan      html  css  js  c++  java
  • 等比缩放算法

    1、函数

    //   功能: 矩形区缩放到目标矩形区
    //   参数:
    //        rcDest    : 目标矩形区域
    //         nSrcWidth: 被缩放矩形区的宽度
    //           nFormat: 矩形区在目标矩形区对齐方式 DT_TOP DT_VCENTER DT_BOTTOM DT_LEFT DT_CENTER DT_RIGHT
    // 返回值: 矩形区缩放后在目标矩形区的位置
    //#define DT_TOP              0x00000000 // 垂直方向顶端对齐
    //#define DT_LEFT             0x00000000 // 水平方向左对齐
    //#define DT_CENTER           0x00000001 // 水平方向居中对齐
    //#define DT_RIGHT            0x00000002 // 水平方向右对齐
    //#define DT_VCENTER          0x00000004 // 垂直方向居中对齐
    //#define DT_BOTTOM           0x00000008 // 垂直方向底部对齐
    CRect Zoom(CRect rcDest, int nSrcWidth, int nSrcHeight, UINT nFormat)
    {
        float fW = 1.0 * nSrcWidth / rcDest.Width();
        float fH = 1.0 * nSrcHeight / rcDest.Height();
        float fRatio = fW;
        if ( fH - fW > 0.000001 && fH - 1 > 0.00001 )
            fRatio = fH;

        nSrcWidth *= fRatio;
        nSrcHeight*= fRatio;
        CRect rc;
        if ( 0 == nFormat )
        {
            rc.top        = rcDest.top;
            rc.bottom    = rc.top + nSrcHeight;
        }
        else if ( 0 != nFormat&0x00000001 )
        {
            rc.top        = rcDest.top + (rcDest.Height() - nSrcHeight)/2;
            rc.bottom    = rc.top + nSrcHeight;
        }
        else if ( 0 != nFormat&0x00000008 )
        {
            rc.bottom    = rcDest.bottom;
            rc.top        = rc.bottom - nSrcHeight;
        }

        if ( 0 == nFormat )
        {
            rc.left = rcDest.left;
            rc.right= rc.left + nSrcWidth;
        }
        else if ( 0 != nFormat&0x00000001 )
        {
            rc.left = rcDest.left + (rcDest.Width()-nSrcWidth)/2;
            rc.right= rc.left + nSrcWidth;
        }
        else if ( 0 != nFormat&0x00000002 )
        {
            rc.right = rcDest.right;
            rc.left  = rc.right - nSrcWidth;
        }
        return rc;
    }

    2、使用示例

    例如将一张300*200的位图缩放到CRect(100, 50, 500, 300)区域中,并且位图在目标区域中垂直与水平方向都居中对齐

    CRect rc = Zoom(CRect(100, 50, 500, 300), 300,200, DT_VCENTER | DT_CENTER);

  • 相关阅读:
    JAVA 面向对象的扩展 内部类
    对于win10 更换JDK后 查询JDK路径还是原路径的解决办法
    懂得的懂
    稀疏数组转化二维数组
    Flume的安装配置
    CentOS7配置ip和ssh免密登录和hadoop环境
    AOP的使用和事务
    spring的个人理解
    单车月结算2-修改和删除功能
    单车月结算1
  • 原文地址:https://www.cnblogs.com/Ffdd/p/5669743.html
Copyright © 2011-2022 走看看