C# 上传文件并生成缩略图

时间:2021-12-15 20:31:35

 

        #region 上传文件并生成缩略图
        /// <summary>
        /// 上传文件并生成缩略图
        /// </summary>
        /// <param name="paramFileUpload">上传控件</param>
        /// <param name="paramVirtualPath">上传目标虚拟路径</param>
        /// <param name="paramDirectory">用户自定义创建文件夹</param>
        /// <param name="paramOriginalDirectory">存放原始文件夹</param>
        /// <param name="paramThumbnailDirectory">存放缩略图文件夹</param>
        /// <param name="tWidth">缩略图默认宽度</param>
        /// <param name="tHeight">缩略图默认高度</param>
        private string UploadAndThumbnail(FileUpload paramFileUpload, string paramVirtualPath, string paramDirectory, string paramOriginalDirectory, string paramThumbnailDirectory, int tWidth, int tHeight, string paramFileName)
        {
            string returnValue = string.Empty;//返回值
            string imgType;//文件类型
            string fullFileName = paramFileUpload.PostedFile.FileName; //上传文件名
            imgType = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();//文件类型
            string UserPath = Server.MapPath(paramVirtualPath).ToString() + "\\" + paramDirectory;//存放文件目录
            //如果文件夹不存在则创建
            if (!Directory.Exists(UserPath))
            {
                Directory.CreateDirectory(UserPath);
            }
            string originalImagePath = UserPath + "\\" + paramOriginalDirectory;//原始路径
            if (!Directory.Exists(originalImagePath))
            {
                Directory.CreateDirectory(originalImagePath);
            }
            string thumbnailPath = UserPath + "\\" + paramThumbnailDirectory;
            if (!Directory.Exists(thumbnailPath))
            {
                Directory.CreateDirectory(thumbnailPath);
            }
            // 获取上传图像的文件名
            string fileName = paramFileName;

            if (imgType == "jpg" || imgType == "jpeg" || imgType == "bmp" || imgType == "png" || imgType == "icon")
            {
                try
                {
                    //生成原图
                    byte[] oFileByte = new byte[paramFileUpload.PostedFile.ContentLength];
                    Stream oStream = paramFileUpload.PostedFile.InputStream;
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
                    int oWidth = oImage.Width;//原图宽度
                    int oHeight = oImage.Height;//原图高度
                    if (tWidth == 0)
                    {
                        tWidth = 100;
                    }
                    if (tHeight == 0)
                    {
                        tHeight = 100;
                    }
                    //按比例计算出缩略图的宽度和高度
                    if (oWidth >= oHeight)
                    {
                        tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oHeight)));
                    }
                    else
                    {
                        tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                    }
                    //生成缩略图
                    Bitmap tImage = new Bitmap(tWidth, tHeight);
                    Graphics g = Graphics.FromImage(tImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    g.Clear(Color.Transparent);//清空画布并以透明背景色填充
                    g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
                    string oPath = originalImagePath + "\\" + fileName + ".jpg";
                    string tPath = thumbnailPath + "\\" + fileName + ".jpg";
                    //以JPG格式保存图像
                    oImage.Save(oPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    tImage.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    returnValue = "上传成功!";
                    //释放资源                    
                    oImage.Dispose();
                    g.Dispose();
                    tImage.Dispose();
                }
                catch (Exception ex)
                {
                    returnValue = "由于网络原因,上载文件错误 " + ex.Message;
                }
            }
            else
            {
                returnValue = "你选择的图像格式错误!";
            }
            return returnValue;
        }
        #endregion