Image.cs: Made GetThumbnailImage throw OutOfMemoryException when image size is invali...
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 5 Jul 2006 12:38:37 +0000 (12:38 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 5 Jul 2006 12:38:37 +0000 (12:38 -0000)
svn path=/trunk/mcs/; revision=62263

mcs/class/System.Drawing/System.Drawing/ChangeLog
mcs/class/System.Drawing/System.Drawing/Image.cs

index 4b9888c78da2fc22b0de93ce81bdf4fdfc4fc000..8e1195a6d962b087cd3a4743043fdd90cd9a7875 100644 (file)
@@ -1,3 +1,9 @@
+2006-07-04  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * Image.cs: Made GetThumbnailImage throw OutOfMemoryException when
+       image size is invalid (to match MS behaviour), also ensure that the
+       Graphics instance is disposed even if CheckStatus throws an exception.
+
 2006-06-26  Sebastien Pouliot  <sebastien@ximian.com>
 
        * Pen.cs: Ensure we dispose of an existing brush before replacing it.
index e23c7402fd5c5c9ce208387cfadd48fbf5b2ded6..5a9a053ebb7b315cbb74a3e71a8bf856045c4e37 100644 (file)
@@ -339,23 +339,23 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                return item;
        }
        
-       public Image GetThumbnailImage(int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData)
+       public Image GetThumbnailImage (int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData)
        {
-               Status          status;
-               Image           ThumbNail;
-               Graphics        g;
+               if ((thumbWidth <= 0) || (thumbHeight <= 0))
+                       throw new OutOfMemoryException ("Invalid thumbnail size");
 
-               ThumbNail=new Bitmap(thumbWidth, thumbHeight);
-               g=Graphics.FromImage(ThumbNail);
-               
-               status = GDIPlus.GdipDrawImageRectRectI(g.nativeObject, nativeObject,
-                                       0, 0, thumbWidth, thumbHeight,
-                                       0, 0, this.Width, this.Height,
-                                       GraphicsUnit.Pixel, IntPtr.Zero, null, IntPtr.Zero);
-                GDIPlus.CheckStatus (status);
-               g.Dispose();
-
-               return(ThumbNail);
+               Image ThumbNail = new Bitmap (thumbWidth, thumbHeight);
+
+               using (Graphics g = Graphics.FromImage (ThumbNail)) {
+                       Status status = GDIPlus.GdipDrawImageRectRectI (g.nativeObject, nativeObject,
+                               0, 0, thumbWidth, thumbHeight,
+                               0, 0, this.Width, this.Height,
+                               GraphicsUnit.Pixel, IntPtr.Zero, null, IntPtr.Zero);
+
+                       GDIPlus.CheckStatus (status);
+               }
+
+               return ThumbNail;
        }