首页 > 其他 > 详细

图片的等比缩放

时间:2016-04-25 00:25:25      阅读:197      评论:0      收藏:0      [点我收藏+]

 

  HttpPostedFile file = context.Request.Files[0];
            using (Stream stream = file.InputStream)
            {
                Image img = new Bitmap(stream);
                //3. 等比缩放图片 按照缩放大的比例来  200 * 100
                int thumbW = 200, thumbH = 100;
                int w = 0, h = 0;
                if (img.Height * 1.0 / thumbH >= img.Width * 1.0 / thumbW)
                {
                    h = thumbH;
                    w = (int)(thumbH * 1.0 / img.Height * img.Width);
                }
                else
                {
                    w = thumbW;
                    h = (int)(thumbW * 1.0 / img.Width * img.Height);
                }

                using (Bitmap newImg = new Bitmap(w, h))
                {
                    using (Graphics graphics = Graphics.FromImage(newImg))
                    {
                        graphics.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

                        //1. 直接裁剪图片
                        //graphics.DrawImage(img, 0, 0, img.Width, img.Height);

                        //2. 指定 缩放 大小  100*100
                        //graphics.DrawImage(img, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

                        newImg.Save(context.Server.MapPath("/b.png"));
                        context.Response.Write("Width: " + img.Width + "  height: " + img.Height);
                    }
                }
            }

 

图片的等比缩放

原文:http://www.cnblogs.com/yougmi/p/5428935.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!