錯誤的代碼g對象繼續占用 未釋放資源 如果路徑不一樣 沒問題 相同路徑 獲取圖片進行
縮略會造成GDI錯誤
1 /// <summary> 2 /// 生成縮略圖 3 /// </summary> 4 /// <param name="originalImagePath">源圖路徑(物理路徑)</param> 5 /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param> 6 /// <param name="width">縮略圖寬度</param> 7 /// <param name="height">縮略圖高度</param> 8 9 public void CreateMinImage(string originalImagePath, string thumbnailPath, int width, int height) 10 { 11 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath)); 12 13 int towidth = width; 14 int toheight = height; 15 16 int ow = originalImage.Width; 17 int oh = originalImage.Height; 18 19 //新建一個bmp圖片 20 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 21 22 //新建一個畫板 23 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 24 25 26 //設置高質量插值法 27 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 28 29 //設置高質量,低速度呈現平滑程度 30 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 31 32 //清空畫布並以透明背景色填充 33 g.Clear(System.Drawing.Color.Transparent); 34 35 //在指定位置並且按指定大小繪制原圖片的指定部分 36 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight)); 37 38 try 39 { 40 //以jpg格式保存縮略圖 41 string FileExt = Path.GetFileNameWithoutExtension(originalImagePath); 42 //這里報錯 bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 43 } 44 catch (System.Exception e) 45 { 46 throw e; 47 } 48 finally 49 { 50 originalImage.Dispose(); 51 bitmap.Dispose(); 52 g.Dispose(); 53 } 54 55 } 56 57 //修改后的代碼 58 public void CreateMinImageAndDel(string originalImagePath, string thumbnailPath, int width, int height) 59 { 60 Graphics draw = null; 61 string FileExt = ""; 62 63 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath)); 64 65 int towidth = width; 66 int toheight = height; 67 68 int ow = originalImage.Width; 69 int oh = originalImage.Height; 70 71 //新建一個bmp圖片 72 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 73 System.Drawing.Image bitmap2 = new System.Drawing.Bitmap(towidth, toheight); 74 //新建一個畫板 75 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 76 77 78 //設置高質量插值法 79 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 80 81 //設置高質量,低速度呈現平滑程度 82 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 83 84 //清空畫布並以透明背景色填充 85 g.Clear(System.Drawing.Color.Transparent); 86 87 //在指定位置並且按指定大小繪制原圖片的指定部分 88 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight)); 89 90 try 91 { 92 //以jpg格式保存縮略圖 93 FileExt = Path.GetFileNameWithoutExtension(originalImagePath); 94 //用新建立的image對象拷貝bitmap對象 讓g對象可以釋放資源 95 draw = Graphics.FromImage(bitmap2); 96 draw.DrawImage(bitmap, 0, 0); 97 98 } 99 catch (System.Exception e) 100 { 101 throw e; 102 } 103 finally 104 { 105 originalImage.Dispose(); 106 bitmap.Dispose(); 107 g.Dispose(); 108 //保存調整在這里即可 109 bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 110 } 111 }
