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

  • 相关阅读:
    Visual C#使用DirectX实现视频播放
    sql server中新增一条数据后返回该数据的ID
    JQuery上传插件Uploadify使用详解
    .NET JS escape加密的后台解密
    Redis Sentinel(哨兵模式)
    Redis集群入门
    Redis 4.0.X版本reshard出现错误的解决办法
    首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法
    Centos6 Ruby 1.8.7升级至Ruby 2.3.1的方法
    SQL Server 2008示例数据库安装问题解决
  • 原文地址:https://www.cnblogs.com/Ffdd/p/5669743.html
Copyright © 2011-2022 走看看