文章类型: .NET
关键词: asp.net,上传,图片,文件,自动修改,图片,大小,代码
内容摘要: asp.net上传图片文件自动修改图片大小代码

asp.net上传图片文件自动修改图片大小代码

2015/7/28 17:59:37    来源:apple    阅读:
#region 图片缩放
    /// <summary>
      /// 图片缩放
    /// </summary>
    /// <param name="savePath">图片相对路径</param>
    /// <param name="fileName">图片名称</param>
    /// <param name="destWidth">缩放宽度</param>
    /// <param name="destHeight">高度</param>
     /// <param name="type">1--固定缩放;2--按比例缩放;3--指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传;4--原图直接上传</param>
     /// <returns></returns>
      public static void ReducesPic(string savePath,string fileName, int destWidth, int destHeight, int type)
      {
          if (!fileName.Equals(""))
          {
              string Allpath = System.Web.HttpContext.Current.Server.MapPath("/") + savePath;
              //生成原图
              System.IO.Stream stream = System.IO.File.OpenRead(Allpath + fileName);
              System.Drawing.Image oImage = System.Drawing.Image.FromStream(stream);
              stream.Close();
              stream.Dispose();
 
              System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
 
              string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
              int oWidth = oImage.Width;
              int oHeight = oImage.Height;
 
              int tWidth = destWidth; //设置缩略图初始宽度
              int tHeight = destHeight; //设置缩略图初始高度
 
              //按指定宽高缩放
              if (type == 1)
              {
                  tWidth = destWidth;
                  tHeight = destHeight;
              }
              //按比例计算出缩略图的宽度和高度
              else if (type == 2)
              {
                  if (oWidth > tWidth || oHeight > tHeight)
                  {
                      if (oWidth >= oHeight)
                      {
                          tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
                      }
                      else
                      {
                          tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                      }
                  }
                  else
                  {
                      tWidth = oWidth; //原图宽度
                      tHeight = oHeight; //原图高度
                  }
              }
              //指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传
              else if (type == 3)
              {
                  if (oWidth >= tWidth)
                  {
                      if (oWidth >= oHeight)
                      {
                          tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
                      }
                      else
                      {
                          tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                      }
                  }
                  else
                  {
                      tWidth = oWidth; //原图宽度
                      tHeight = oHeight; //原图高度
                  }
 
              }
              else
              {
                  tWidth = oWidth; //原图宽度
                  tHeight = oHeight; //原图高度
              }
              //生成缩略原图
              oImage = oImage.GetThumbnailImage(tWidth, tHeight, callb, IntPtr.Zero);
              oImage.Save(Allpath+fileName);
 
          }
         
 
      }
      public static bool ThumbnailCallback() { return false; }
      #endregion


↑ 上一篇文章:ASP.NET中的URL编码解码 关键词:ASP.NET,URL编码,解码 发布日期:2015/7/28 17:58:13
↓ 下一篇文章:js实现分页的思路代码 关键词:js,javascript,分页,思路 发布日期:2015/7/29 9:13:54
相关文章:
ASP.NET(C#)实现一次性上传多张图片(多个文件) 关键词:ASP.NET,C#,一次性,上传,多张图片,多个文件 发布日期:2015-07-28 16:07
使用UMEDITOR上传图片一直提示上传失败的解决办法 关键词:UMeditor,.net,图片上传失败,Uploader,解决,方法 发布日期:2016-05-15 00:29
asp.net 修改/删除站内目录操作后Session丢失问题 关键词:asp,.net,修改,删除,站内目录,Session,丢失,文件,上传,删除 发布日期:2015-07-29 14:56
相关目录:.NET软件开发HTMLANDROID
我要评论
正在加载评论信息......