等比缩放算法

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

 

转载于:https://www.cnblogs.com/Ffdd/p/5669743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值