错误的代码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 }
时间: 2024-10-10 04:47:24