• 等比缩放算法


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

  • 相关阅读:
    移除HTML5 input在type="number"时的上下小箭头
    JQUERY 实现加入收藏夹功能
    发现移动端在uc浏览器上会放大bug,解决此bug的方法!
    HTML 定时页面跳转
    Some Commands I Used Frequently
    Some Life Tricks I Noticed
    几种工厂模式的区别
    大型DELETE(删除大量数据)的一种解决方案
    笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-10 可编程对象
    笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-09 事务和并发
  • 原文地址:https://www.cnblogs.com/Ffdd/p/5669743.html
Copyright © 2020-2023  润新知