juliet_zl

juliet_zl

  • NA
  • 37
  • 0

Releasing GDI+ resources

Feb 4 2004 12:53 AM
Dear friends, I used the following routine to do an image rescaling: private void CreateMediumImage(Size n_size) { Bitmap SourceBitmap = new Bitmap(OriginalImagePath); Bitmap TargetBitmap = new Bitmap(n_size.Width, n_size.Height); Graphics bmpGraphics = Graphics.FromImage(TargetBitmap); // set Drawing Quality bmpGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; bmpGraphics.SmoothingMode = SmoothingMode.HighQuality; Rectangle compressionRectangle = new Rectangle(0, 0, n_size.Width, n_size.Height); bmpGraphics.DrawImage(SourceBitmap, compressionRectangle); // decide new path and save the image WriteToFile(TargetBitmap, this.target_folder, this.n_file_name); bmpGraphics.Dispose(); TargetBitmap.Dispose(); SourceBitmap.Dispose(); } and the WriteToFile() routine contains: FileStream fs = new FileStream(folder + fn, FileMode.Create, FileAccess.Write); im.Save(fs, ImageFormat.Bmp); fs.Close(); See that I have already dispose the graphics object and both of the bitmap object at the end of the routine. I use a PictureBox control to display the image on a windows form then: this.imgProductImage.Image = System.Drawing.Image.FromFile(fn); However, when the same image is needed to be deleted later in the program, the following error occurs: The process cannot access the file "E:\Documents and Settings\juliet\Desktop\garment\garment\bin\Release\product_image\product_00001.bmp" because it is being used by another process. Obviously, the resources being held by the program is not released well. However, I cannot find what else should I dispose. Please kindly offer any suggestion. Thanks in advanced.

Answers (1)