zoukankan      html  css  js  c++  java
  • 图片的等比缩放

      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);
                        }
                    }
                }
  • 相关阅读:
    Linq101-Generation
    Linq101-Element
    解决 oracle 错误ORA-01033
    Oracle数据表恢复
    C++构造函数
    C++类与对象
    CMake命令之export
    CMake命令之install
    CMake变量(提供信息的变量)
    CMake常用变量
  • 原文地址:https://www.cnblogs.com/yougmi/p/5428935.html
Copyright © 2011-2022 走看看