zoukankan      html  css  js  c++  java
  • 按比例生成缩略图

     1 public static void saveTwoImages(string imgName,string simgName,string strPath,System.Web.UI.HtmlControls.HtmlInputFile imageUp)
     2   {
     3    //生成原图
     4    Byte[] oFileByte = new byte[imageUp.PostedFile.ContentLength];
     5    System.IO.Stream oStream = imageUp.PostedFile.InputStream;
     6    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
     7    int oWidth = oImage.Width; //原图宽度
     8    int oHeight = oImage.Height; //原图高度
     9    int tWidth = 100; //设置缩略图初始宽度
    10    int tHeight = 100; //设置缩略图初始高度
    11    string imgExt = getFileExtName(imageUp);
    12    //按比例计算出缩略图的宽度和高度
    13    if(oWidth >= oHeight)
    14    {
    15     tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
    16    }
    17    else
    18    {
    19     tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
    20    }
    21    //生成缩略原图
    22    Bitmap tImage = new Bitmap(tWidth,tHeight);
    23    Graphics g = Graphics.FromImage(tImage);
    24    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
    25    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
    26    g.Clear(Color.Transparent); //清空画布并以透明背景色填充
    27    g.DrawImage(oImage,new Rectangle(0,0,tWidth,tHeight),new Rectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
    28    string oFullName = strPath + imgName; //保存原图的物理路径
    29    string tFullName = strPath + simgName; //保存缩略图的物理路径
    30    try
    31    {
    32     //以JPG格式保存图片
    33     switch(imgExt.ToLower())
    34     {
    35      case "jpg":
    36       oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
    37       tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);//
    38       break;
    39      case "gif":
    40       oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Gif);
    41       tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Gif);
    42       break;
    43      case "png":
    44       oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Bmp);
    45       tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Bmp);
    46       break;
    47     }
    48    }
    49    catch(Exception ex)
    50    {
    51     throw ex;
    52    }
    53    finally
    54    {
    55     //释放资源
    56     oImage.Dispose();
    57     g.Dispose();
    58     tImage.Dispose();
    59    }
    60   }

    使用方法:

    string strTime=General.getDateTime();
         string strExt=General.getFileExtName(File1);
         strPicture=strUserId+"ShopZ"+strTime+"."+strExt;
         strSPicture=strUserId+"ShopS"+strTime+"."+strExt;
         string strFilePath=Server.MapPath("Images/");
         try
         {
          saveTwoImages(strPicture,strSPicture,strFilePath,File1);
         }
  • 相关阅读:
    2019-2020-1 20175228 实验四 外设驱动程序设计
    2019-2020-1 20175228 实验三 实时系统
    2019-2020-1-20175332 20175323 20175228 实验一开发环境的熟悉
    2018-2019-2 20175228实验五《Java网络编程》实验报告
    2018-2019-2 20175228实验四《Android开发基础》实验报告
    2018-2019-2 20175228实验三《敏捷开发与XP实践》实验报告
    MyCP
    2018-2019-2 20175228实验二《面向对象程序设计》实验报告
    2018-2019-2 20175228实验一《Java开发环境的熟悉》实验报告
    转()析构函数
  • 原文地址:https://www.cnblogs.com/Chaser-Eagle/p/3684942.html
Copyright © 2011-2022 走看看