標籤:des c style class blog code
本文轉自:http://www.cnblogs.com/promic/archive/2010/04/21/1717190.html
private static string strConnect = System.Configuration.ConfigurationManager.AppSettings["connStr"]; //上傳映像 protected void btnUpload_Click(object sender, EventArgs e) { …… string imgType; string fullFileName = this.txtPath.PostedFile.FileName; imgType = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower(); string UserDirectory = Session["UserID"].ToString();//使用者ID為所建立檔案夾的名字 string UserPath = Server.MapPath("UpLoad").ToString() + "\\" + UserDirectory; //如果檔案夾不存在則建立 if (!Directory.Exists(UserPath)) { Directory.CreateDirectory(UserPath); } string originalImagePath = UserPath + "\\" + "Original"; if(!Directory.Exists(originalImagePath)) { Directory.CreateDirectory(originalImagePath); } string thumbnailPath = UserPath + "\\" + "Thumbnail"; if (!Directory.Exists(thumbnailPath)) { Directory.CreateDirectory(thumbnailPath); } // 擷取上傳映像的檔案名稱 string fileName = txtName.Text; …… if (imgType=="jpg"||imgType=="jpeg"||imgType=="bmp"||imgType=="png"||imgType=="icon") { try { //產生原圖 byte[] oFileByte = new byte[this.txtPath.PostedFile.ContentLength]; Stream oStream = this.txtPath.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width;//原圖寬度 int oHeight = oImage.Height;//原圖高度 int tWidth = 100;//設定縮圖初始寬度 int 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); //this.txtPath.PostedFile.SaveAs(originalImagePath + "\\" + fileName + "." + imgType); //string originalImageUrl = "UpLoad/" + UserDirectory + "/" + "Orginal/"+fileName + ".jpg" ;//得到服務端原圖片的虛擬路徑 // string thumbnailImageUrl = "UpLoad/" + UserDirectory + "/" + "Thumbnail/" + fileName + ".jpg";//得到服務端原圖片的虛擬路徑 //以JPG格式儲存映像 //oImage.Save(originalImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg);/*看看這裡這兩個儲存方式有沒有問題*/ // tImage.Save(thumbnailImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg); 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); lblMessage.Visible = true; lblMessage.Text = "映像上傳成功!"; txtName.Text = ""; txtDescription.Text = ""; //釋放資源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } catch (Exception ex) { lblMessage.Visible = true; lblMessage.Text = "由於網路原因,上傳檔案錯誤 " + ex.Message; } } else { Response.Write("<script language=‘javascript‘>alert(‘你選擇的映像格式錯誤!‘);</script>"); lblMessage.Visible = false; }}