1. 程式人生 > >C# 上傳檔案並生成縮圖

C# 上傳檔案並生成縮圖




        #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