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


      #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
    

      

  • 相关阅读:
    yum clean all大坑解决
    RHEL 7 “There are no enabled repos” 的解决方法
    exportfs命令 – 管理NFS服务器共享的文件系统
    Linux放大缩小字体的快捷键
    chcon命令详解
    通过配置hosts.allow和hosts.deny文件允许或禁止ssh或telnet操作
    安装RHEL7配置本地yum源 -- yum不能安装时,在本地安装,亲测成功
    块存储、文件存储、对象存储意义及差异
    在Windows Server 2012 R2域环境中禁用(取消)密码复杂策略
    bat脚本静默安装软件示例
  • 原文地址:https://www.cnblogs.com/fogwang/p/3930831.html
Copyright © 2020-2023  润新知