幫助類html
http://www.cnblogs.com/Kiss920Zz/archive/2011/12/26/2302223.html 作了個在上傳圖片的時候,自動修改圖片大小的功能,其實一開始是想作相似京東商城的放大鏡,而後發現作那樣的須要相同的圖片大小不一樣的的三張,因而想到是否是能夠再上傳圖片的時候只上傳一張,自動生成三張大小的圖片,最後就作了這個功能! #region 圖片上傳 /// <summary> /// 圖片上傳0 /// </summary> /// <returns></returns> private string Upload_Images0() { Boolean fileOK = false; string imageFile = ""; String path = Server.MapPath("~/adpic/"); //設置服務器上傳的路徑,即文件上傳的位置 if (fulImage.PostedFile.ContentLength / 1024 < 2000) { if (fulImage.HasFile) { //獲取上傳文件類型 String fileExtension = System.IO.Path.GetExtension(fulImage.FileName).ToLower(); //判斷容許上傳圖片類型 String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; break; } } if (fileOK) { try { //把當前時間取出,組成字符串,加入文件名稱,防止重複命名 string fileName = DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "").Replace("/", "") + fulImage.FileName; fulImage.PostedFile.SaveAs(path + fileName); imageFile = "adpic/" + fileName; //自動生成小圖 MakeThumNail(path + fileName, Server.MapPath("~/imagess/") + fileName, 600, 500, "W"); //自動生成小圖 MakeThumNail(path + fileName, Server.MapPath("~/image/") + fileName, 100, 50, "W"); //自動生成中圖 MakeThumNail(path + fileName, Server.MapPath("~/images/") + fileName, 200, 50, "W"); //刪除原圖 System.IO.File.Delete(path + fileName); } catch (Exception e) { Response.Write("<script>alert('對不起,圖片不能上傳!')</script>"); } } else { Response.Write("<script>alert('對不起,不能接受這種文件類型!')</script>"); } } } else { Response.Write("<script>alert('對不起,圖片過大不能上傳!')</script>"); } return imageFile; } #endregion # region 圖片處理 /// <summary> /// 縮放圖像 /// </summary> /// <param name="originalImagePath">圖片原始路徑</param> /// <param name="thumNailPath">保存路徑</param> /// <param name="width">縮放圖的寬</param> /// <param name="height">縮放圖的高</param> /// <param name="model">縮放模式</param> public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int thumWidth = width; //縮略圖的寬度 int thumHeight = height; //縮略圖的高度 int x = 0; int y = 0; int originalWidth = originalImage.Width; //原始圖片的寬度 int originalHeight = originalImage.Height; //原始圖片的高度 switch (model) { case "HW": //指定高寬縮放,可能變形 break; case "W": //指定寬度,高度按照比例縮放 thumHeight = originalImage.Height * width / originalImage.Width; break; case "H": //指定高度,寬度按照等比例縮放 thumWidth = originalImage.Width * height / originalImage.Height; break; case "Cut": if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight) { originalHeight = originalImage.Height; originalWidth = originalImage.Height * thumWidth / thumHeight; y = 0; x = (originalImage.Width - originalWidth) / 2; } else { originalWidth = originalImage.Width; originalHeight = originalWidth * height / thumWidth; x = 0; y = (originalImage.Height - originalHeight) / 2; } break; default: break; } //新建一個bmp圖片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight); //新建一個畫板 System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap); //設置高質量查值法 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設置高質量,低速度呈現平滑程度 graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空畫布並以透明背景色填充 graphic.Clear(System.Drawing.Color.Transparent); //在指定位置而且按指定大小繪製原圖片的指定部分 graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); bitmap.Dispose(); graphic.Dispose(); } } # endregion