zoukankan      html  css  js  c++  java
  • C# 将前端传来的图片文件分别以大图和缩略图保存

        HttpPostedFile pic_upload = Request.Files["file"];
          Bitmap bitmap = (Bitmap)System.Drawing.Image.FromStream(pic_upload.InputStream);
          Size s = new Size();
          s.Height = 50;
          s.Width = 50;
          System.Drawing.Image minImage =  clsPublic.GetImageThumb(bitmap, s);    

      //下面这部分不重要,主要内容是在上面,有缩略图和原图后,直接按照常规的方法进行保存即可

    string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
    string filepath = "~/upload/" + kis_web.DBHelper.sAcctNumber + "/files/";//图片存储路径:uppad+账套编号+images
    string activitecode = Guid.NewGuid().ToString().Replace("-", "");
    if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
    {
    Directory.CreateDirectory(Server.MapPath(filepath));
    }
    string virpath = filepath + activitecode + fileExtension;//这是存到服务器上的虚拟路径
    string virMinpath = filepath + activitecode+"_min" + fileExtension;

     转缩略图方法

      public static Bitmap GetImageThumb(Bitmap mg, Size newSize)
      {
        double ratio = 0d;
        double myThumbWidth = 0d;
        double myThumbHeight = 0d;
        int x = 0;
        int y = 0;
    
        Bitmap bp;
        if (newSize.Width == 0)
        {
          newSize.Width = 1;
        }
        if (newSize.Height == 0)
        {
          newSize.Height = 1;
        }
        if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
        Convert.ToDouble(newSize.Height)))
          ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
        else
          ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
        myThumbHeight = Math.Ceiling(mg.Height / ratio);
        myThumbWidth = Math.Ceiling(mg.Width / ratio);
    
        Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
        bp = new Bitmap(newSize.Width, newSize.Height);
        x = (newSize.Width - thumbSize.Width) / 2;
        y = (newSize.Height - thumbSize.Height);
        System.Drawing.Graphics g = Graphics.FromImage(bp);
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
        g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);
        return bp;
    
      }

    技术参考:https://www.cnblogs.com/zhangchaoran/p/7693166.html

  • 相关阅读:
    通过securecrt跳板机登录linux服务器
    python2.x提示这个错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position
    使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
    linux 中文乱码解决
    工作中常用到的命令
    LR学习文档整理
    Django入门示例之被解放的姜戈——02 庄园疑云(数据库及模型)
    Django入门示例之被解放的姜戈——01 初试天涯(安装及启动)
    格式化字符串format函数
    Python之print 格式化输出
  • 原文地址:https://www.cnblogs.com/ncellit/p/10096639.html
Copyright © 2011-2022 走看看