2006-08-07 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Image.cs
index bd91ce348b72bfddbea5163eabc46b2b3cfd230c..a2371aa8159395eaee69506ada4912ab9dfcec83 100644 (file)
@@ -86,7 +86,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
                MemoryStream ms = new MemoryStream ();
-               this.Save (ms, ImageFormat.Bmp);
+               this.Save (ms, RawFormat);
                info.AddValue ("Data", ms.ToArray ());
        }
     
@@ -140,15 +140,12 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                return new Bitmap (stream, useECM);
        }
 
-#if NET_2_0
-
        // See http://support.microsoft.com/default.aspx?scid=kb;en-us;831419 for performance discussion        
-       public static Image FromStream (Stream stream, bool useECM, bool validateImageData)\r
+       public static Image FromStream (Stream stream, bool useECM, bool validateImageData)
        {
                return new Bitmap (stream, useECM);
        }
 
-#endif
        public static int GetPixelFormatSize(PixelFormat pixfmt)
        {
                int result = 0;
@@ -332,7 +329,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                try {
                        status = GDIPlus.GdipGetPropertyItem (nativeObject, propid, propSize, property);
                        GDIPlus.CheckStatus (status);
-                       gdipProperty = (GdipPropertyItem) Marshal.PtrToStructure ((IntPtr)property, 
+                       gdipProperty = (GdipPropertyItem) Marshal.PtrToStructure (property, 
                                                                typeof (GdipPropertyItem));                                             
                        GdipPropertyItem.MarshalTo (gdipProperty, item);
                }
@@ -342,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();
+               Image ThumbNail = new Bitmap (thumbWidth, thumbHeight);
 
-               return(ThumbNail);
+               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;
        }
        
        
@@ -708,10 +705,10 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
        }
 
 #if NET_2_0
-
-       [LocalizableAttribute(false)] \r
+       [DefaultValue (null)]
+       [LocalizableAttribute(false)] 
        [BindableAttribute(true)]       
-       [TypeConverter (typeof (StringConverter))]\r
+       [TypeConverter (typeof (StringConverter))]
        public object Tag { 
                get { return tag; }
                set { tag = value; }
@@ -761,38 +758,28 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                Dispose (false);
        }
 
-       private void DisposeResources ()
-       {
-               Status status = GDIPlus.GdipDisposeImage (nativeObject);
-               GDIPlus.CheckStatus (status);           
-       }
-       
        protected virtual void Dispose (bool disposing)
        {
                if (nativeObject != IntPtr.Zero){
-                       DisposeResources ();
+                       Status status = GDIPlus.GdipDisposeImage (nativeObject);
+                       // set nativeObject to null before throwing an exception
                        nativeObject = IntPtr.Zero;
+                       GDIPlus.CheckStatus (status);           
                }
        }
        
-       public virtual object Clone()
-       {                               
+       public object Clone ()
+       {
+               Bitmap b = (this as Bitmap);
+               if (b == null)
+                       throw new NotImplementedException ("This Image instance isn't a Bitmap instance."); 
 
                IntPtr newimage = IntPtr.Zero;
-               
-               if (!(this is Bitmap)) 
-                       throw new NotImplementedException (); 
-               
                Status status = GDIPlus.GdipCloneImage (NativeObject, out newimage);                    
                GDIPlus.CheckStatus (status);                   
 
-               if (this is Bitmap){
-                       return new Bitmap (newimage);
-               }
-               
-               throw new NotImplementedException ();           
+               return new Bitmap (newimage);
        }
-
 }
 
 }