24小时热门版块排行榜    

查看: 143  |  回复: 0
当前主题已经存档。

zsglly

木虫 (著名写手)

[交流] 如何等比例缩放图像

在放大或缩小图像时,如何能最好的保持图像原态比例来显示呢?下面提供一个涵数可帮你来实现,调用它后,返回的矩形区域既为最佳显示图像大小。
l 涵数中的第一个参数rcScreen为图像要被显示的矩形大小,第二个参数sizePicture为图像自身大小,第三个参数bCenter为是否居中显示,返回值CRect既为图像最佳显示大小。
涵数如下所示:
CRect RectSizeWithConstantRatio( CRect* rcScreen,
                                       CSize sizePicture,
                                       BOOL bCenter)
{
  CRect rect(rcScreen);
  double dWidth = rcScreen->Width();
  double dHeight = rcScreen->Height();
  double dAspectRatio = dWidth/dHeight;

  double dPictureWidth = sizePicture.cx;
  double dPictureHeight = sizePicture.cy;
  double dPictureAspectRatio = dPictureWidth/dPictureHeight;

  //If the aspect ratios are the same then the screen rectangle
  // will do, otherwise we need to calculate the new rectangle

  if (dPictureAspectRatio > dAspectRatio)
  {
    int nNewHeight = (int)(dWidth/dPictureWidth*dPictureHeight);
    int nCenteringFactor = (rcScreen->Height() - nNewHeight) / 2;
    rect.SetRect( 0,
                  nCenteringFactor,
                  (int)dWidth,
                  nNewHeight + nCenteringFactor);

  }
  else if (dPictureAspectRatio < dAspectRatio)
  {
    int nNewWidth =  (int)(dHeight/dPictureHeight*dPictureWidth);
    int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;
    rect.SetRect( nCenteringFactor,  
                  0,
                  nNewWidth + nCenteringFactor,
                  (int)(dHeight));
  }

  return rect;
};

[ Last edited by 幻影无痕 on 2006-11-17 at 08:19 ]
回复此楼
做人要厚道啊!厚道啊!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zsglly 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 jCd0dEvKHShX 2026-08-29 4/200 2026-08-30 19:13 by hZiFeudZyoGR
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 G6APbkg8SA6w 2026-08-29 5/250 2026-08-30 18:51 by hZiFeudZyoGR
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 G6APbkg8SA6w 2026-08-29 4/200 2026-08-30 18:51 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 G6APbkg8SA6w 2026-08-29 5/250 2026-08-30 18:40 by hZiFeudZyoGR
[博后之家] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 gy1nBQXYQJqL 2026-08-29 4/200 2026-08-30 18:28 by hZiFeudZyoGR
[论文投稿] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +6 gy1nBQXYQJqL 2026-08-29 7/350 2026-08-30 18:18 by hZiFeudZyoGR
[公派出国] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +7 ASdOkHsho7FD 2026-08-28 9/450 2026-08-30 17:34 by hZiFeudZyoGR
[论文投稿] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +5 ASdOkHsho7FD 2026-08-28 6/300 2026-08-30 17:25 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +7 ASdOkHsho7FD 2026-08-28 14/700 2026-08-30 17:01 by hZiFeudZyoGR
[考博] 找导师 +6 yuanjiabao 2026-08-29 7/350 2026-08-30 14:40 by 生科新手
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +6 gdd2018 2026-08-28 11/550 2026-08-30 08:58 by 超级无敌华子
[基金申请] 国自然面上复盘~欢迎讨论 (金币+15) +15 晴天加油 2026-08-26 16/800 2026-08-29 18:28 by symmetry
[基金申请] 哪位高人中了,把查询到的截图贴出来让我看看,让我长长见识 +5 yuleib84 2026-08-26 6/300 2026-08-28 00:02 by yudaoqian88
[基金申请] 面上合作单位盖章 +5 ssyjh 2026-08-27 5/250 2026-08-27 20:50 by gdfollow
[基金申请] 怎么看青基中了没有啊 +5 叶九微 2026-08-26 5/250 2026-08-27 10:35 by l_zh2008
[基金申请] 我不理解! +15 Edward_pc 2026-08-26 23/1150 2026-08-26 20:34 by zzuzxg
[基金申请] 国合里面能看到了 +7 一怀馨秋 2026-08-26 7/350 2026-08-26 11:23 by zhaosm1982
[基金申请] 明天应该可查了!? +6 chengyan1220 2026-08-23 6/300 2026-08-25 19:45 by zfd97
[基金申请] 如果此刻你正在为国基感到焦虑,不妨来听听这首《基金之外》 +8 scalable 2026-08-24 8/400 2026-08-25 12:52 by jnhyjjm
[基金申请] 没有任何消息-是不是就凉了 +9 图啦图啦 2026-08-24 10/500 2026-08-25 11:59 by 南海小哥
信息提示
请填处理意见