24小时热门版块排行榜    

查看: 144  |  回复: 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文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 jCd0dEvKHShX 2026-08-29 4/200 2026-08-30 19:57 by hZiFeudZyoGR
[找工作] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +4 jCd0dEvKHShX 2026-08-29 5/250 2026-08-30 19:48 by hZiFeudZyoGR
[硕博家园] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 jCd0dEvKHShX 2026-08-29 5/250 2026-08-30 19:35 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 G6APbkg8SA6w 2026-08-29 4/200 2026-08-30 18:53 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,科目全,可伽急 +3 G6APbkg8SA6w 2026-08-29 5/250 2026-08-30 18:40 by hZiFeudZyoGR
[论文投稿] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +5 gy1nBQXYQJqL 2026-08-29 5/250 2026-08-30 18:29 by hZiFeudZyoGR
[公派出国] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +6 gy1nBQXYQJqL 2026-08-29 9/450 2026-08-30 17:56 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +4 gy1nBQXYQJqL 2026-08-29 7/350 2026-08-30 17:45 by hZiFeudZyoGR
[考研] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +7 ASdOkHsho7FD 2026-08-28 10/500 2026-08-30 17:33 by hZiFeudZyoGR
[找工作] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +6 ASdOkHsho7FD 2026-08-28 9/450 2026-08-30 11:19 by l0VvVHGBGRLv
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +6 gdd2018 2026-08-28 11/550 2026-08-30 08:58 by 超级无敌华子
[考研] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 gy1nBQXYQJqL 2026-08-29 4/200 2026-08-30 06:57 by ZPa0EcMwuECS
[基金申请] 系统查不到 +11 董八千 2026-08-26 11/550 2026-08-28 18:06 by Leogzhya
[基金申请] 面上合作单位盖章 +5 ssyjh 2026-08-27 5/250 2026-08-27 20:50 by gdfollow
[基金申请] 为什么 国际(地区)合作与交流项目 没有放榜? 10+3 majunge000 2026-08-26 11/550 2026-08-27 08:42 by 北京莱茵编辑
[基金申请] 我不理解! +15 Edward_pc 2026-08-26 23/1150 2026-08-26 20:34 by zzuzxg
[基金申请] 国际合作可查了,中了面上 (EPI+1)(金币+50) +18 Ldrop2023 2026-08-26 18/900 2026-08-26 11:15 by cmrandy
[基金申请] 国合可查了 +3 paperzjh 2026-08-26 3/150 2026-08-26 10:41 by LemmonTr
信息提示
请填处理意见