gdi+ 图片缩放算法

内切图片

 /// <summary>
        /// 内切正方形图片算法
        /// </summary>
        /// <param name="b">原始图片</param>
        /// <returns>图片内切区域</returns>

 public System.Drawing.RectangleF Zoom1Image(System.Drawing.Image b)

        {
            System.Drawing.PointF pt = new System.Drawing.PointF(0, 0);
            System.Drawing.SizeF size = new System.Drawing.SizeF(b.Width, b.Height);
            //float rate = 1f;
            if (b.Width <= b.Height)
            {
                //rate = smallw / b.Width;
                //size.Height = b.Height * rate;
                //pt.Y = (b.Height - size.Height) / 2f;
                size.Height = b.Width;
                pt.Y = (b.Height - b.Width) / 2;
            }
            else
            {//高度全部
                size.Width = b.Height;
                //rate = smallw / b.Height;
                //size.Width = b.Width * rate;
                pt.X = (b.Width - b.Height) / 2f;
            }
            return new System.Drawing.RectangleF(pt, size);
        }


/// <summary>
        /// 图片外切缩放
        /// </summary>
        /// <param name="b"></param>
        /// <returns>正方形内图片所占区域</returns>
        public System.Drawing.RectangleF Zoom2Image(System.Drawing.Image b)
        {

float smallw = 200;//图片宽度
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0,0,smallw,smallw);
            if (b.Width > b.Height)
            {
                float rate = smallw / b.Width;
                float height = rate * b.Height;
                float py = (smallw - height)/2;
                rect = new System.Drawing.RectangleF(0, py, smallw, height);
            }
            else
            {


                float rate = smallw / b.Height;
                float width = rate * b.Width;
                float px = (smallw - width) / 2;
                rect = new System.Drawing.RectangleF(px, 0, width, smallw);
            }
            return rect;
        }


/// <summary>
        /// 等比例缩放
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public System.Drawing.SizeF ZoomImage(System.Drawing.Image b)
        {
            var s = new System.Drawing.SizeF(b.Width, b.Height);
            if (b.Width > standW || b.Height > standW)
            {
                float rate = 1f;
                float wr = standW / b.Width;
                float hr = standW / b.Height;
                float width = b.Width;
                float height = b.Height;
                if (wr >= hr)
                {
                    rate = wr;
                }
                else
                {
                    rate = hr;
                }
                width = b.Width * rate;
                height = b.Height * rate;
            }
            return s;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值