2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Image.cs
index c11ed6cbe4e92bbde3aaeb82ba1d6c802b53e988..0bfe5c94215db1102aa933a9f7fc273a76e13048 100644 (file)
@@ -264,10 +264,29 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                
        }
        
-       [MonoTODO]      
        public PropertyItem GetPropertyItem(int propid)
        {
-               throw new NotImplementedException ();
+               int propSize;
+               IntPtr property;
+               PropertyItem item = new PropertyItem ();
+               GdipPropertyItem gdipProperty = new GdipPropertyItem ();
+               Status status;
+                       
+               status = GDIPlus.GdipGetPropertyItemSize (nativeObject, propid, 
+                                                                       out propSize);
+               GDIPlus.CheckStatus (status);
+
+               /* Get PropertyItem */
+               property = Marshal.AllocHGlobal (propSize);
+               status = GDIPlus.GdipGetPropertyItem (nativeObject, propid, propSize,  
+                                                                               property);
+               GDIPlus.CheckStatus (status);
+               gdipProperty = (GdipPropertyItem) Marshal.PtrToStructure ((IntPtr)property, 
+                                                               typeof (GdipPropertyItem));                                             
+               GdipPropertyItem.MarshalTo (gdipProperty, item);                                                                
+               
+               Marshal.FreeHGlobal (property);
+               return item;
        }
        
        public Image GetThumbnailImage(int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData)
@@ -383,7 +402,6 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                }
                GDIPlus.CheckStatus (st);
        }
-
        
        public void SaveAdd (EncoderParameters encoderParams)
        {
@@ -391,20 +409,20 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                
                IntPtr nativeEncoderParams = encoderParams.ToNativePtr ();
                st = GDIPlus.GdipSaveAdd (nativeObject, nativeEncoderParams);
-               Marshal.FreeHGlobal (nativeEncoderParams);
+               Marshal.FreeHGlobal (nativeEncoderParams);\r
+               GDIPlus.CheckStatus (st);
        }
-       
-       
+               
        public void SaveAdd (Image image, EncoderParameters encoderParams)
        {
                Status st;
                
                IntPtr nativeEncoderParams = encoderParams.ToNativePtr ();
                st = GDIPlus.GdipSaveAddImage (nativeObject, image.NativeObject, nativeEncoderParams);
-               Marshal.FreeHGlobal (nativeEncoderParams);
+               Marshal.FreeHGlobal (nativeEncoderParams);\r
+               GDIPlus.CheckStatus (st);
        }
-       
-       
+               
        public int SelectActiveFrame(FrameDimension dimension, int frameIndex)
        {
                Guid guid = dimension.Guid;             
@@ -415,10 +433,15 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                return frameIndex;              
        }
        
-       [MonoTODO]      
        public void SetPropertyItem(PropertyItem propitem)
        {
-               throw new NotImplementedException ();
+               IntPtr property;
+               int size = Marshal.SizeOf (typeof(GdipPropertyItem));
+               property = Marshal.AllocHGlobal (size);
+
+               Marshal.StructureToPtr (propitem, property, true);
+               Status status = GDIPlus.GdipSetPropertyItem (nativeObject, property);
+               GDIPlus.CheckStatus (status);
        }
 
        // properties   
@@ -439,7 +462,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                        uint found;
                        Status status = GDIPlus.GdipImageGetFrameDimensionsCount (nativeObject, out found);
                        GDIPlus.CheckStatus (status);
-                       Guid [] guid = new Guid [found ];
+                       Guid [] guid = new Guid [found];
                        status = GDIPlus.GdipImageGetFrameDimensionsList (nativeObject, guid, found);
                        GDIPlus.CheckStatus (status);  
                        return guid;
@@ -480,8 +503,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                        colorPalette = value;                                   
                }
        }
-       
-       
+               
        public SizeF PhysicalDimension {
                get {
                        float width,  height;
@@ -502,20 +524,60 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                }               
        }
        
-       
-       [MonoTODO]
        [Browsable (false)]
        public int[] PropertyIdList {
                get {
-                       throw new NotImplementedException ();
+                       uint propNumbers;
+                       
+                       Status status = GDIPlus.GdipGetPropertyCount (nativeObject, 
+                                                                       out propNumbers);                       
+                       GDIPlus.CheckStatus (status);
+                       
+                       int [] idList = new int [propNumbers];
+                       status = GDIPlus.GdipGetPropertyIdList (nativeObject, 
+                                                               propNumbers, idList);
+                       GDIPlus.CheckStatus (status);
+                       
+                       return idList;
                }
        }
        
-       [MonoTODO]
        [Browsable (false)]
        public PropertyItem[] PropertyItems {
                get {
-                       throw new NotImplementedException ();
+                       int propNums, propsSize, propSize;
+                       IntPtr properties, propPtr;
+                       PropertyItem[] items;
+                       GdipPropertyItem gdipProperty = new GdipPropertyItem ();
+                       Status status;
+                       
+                       status = GDIPlus.GdipGetPropertySize (nativeObject, out propsSize, out propNums);
+                       GDIPlus.CheckStatus (status);
+
+                       items =  new PropertyItem [propNums];
+                       
+                       if (propNums == 0)
+                               return items;                   
+                                       
+                       /* Get PropertyItem list*/
+                       properties = Marshal.AllocHGlobal (propsSize);
+                       status = GDIPlus.GdipGetAllPropertyItems (nativeObject, propsSize, 
+                                                               propNums, properties);
+                       GDIPlus.CheckStatus (status);
+
+                       propSize = Marshal.SizeOf (gdipProperty);                       
+                       propPtr = properties;
+                       
+                       for (int i = 0; i < propNums; i++, propPtr = new IntPtr (propPtr.ToInt64 () + propSize))
+                       {
+                               gdipProperty = (GdipPropertyItem) Marshal.PtrToStructure 
+                                               (propPtr, typeof (GdipPropertyItem));                                           
+                               items [i] = new PropertyItem ();
+                               GdipPropertyItem.MarshalTo (gdipProperty, items [i]);                                                           
+                       }
+                       
+                       Marshal.FreeHGlobal (properties);
+                       return items;
                }
        }
 
@@ -571,6 +633,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
        public void Dispose ()
        {
                Dispose (true);
+               System.GC.SuppressFinalize (this);
        }
 
        ~Image ()
@@ -589,13 +652,12 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
        
        protected virtual void Dispose (bool disposing)
        {
-               if (nativeObject != (IntPtr) 0){
+               if (nativeObject != IntPtr.Zero){
                        DisposeResources ();
-                       nativeObject=IntPtr.Zero;
+                       nativeObject = IntPtr.Zero;
                }
        }
        
-       
        public virtual object Clone()
        {                               
                lock (this)
@@ -606,11 +668,16 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
                                throw new NotImplementedException (); 
                        
                        Status status = GDIPlus.GdipCloneImage (NativeObject, out newimage);                    
-                       
                        GDIPlus.CheckStatus (status);                   
-                       
-                       if (this is Bitmap)
-                               return new Bitmap (newimage);
+
+                       if (this is Bitmap){
+                               Bitmap b = new Bitmap (newimage);
+
+                               if (colorPalette != null)
+                                       b.colorPalette = colorPalette.Clone ();
+
+                               return b;
+                       }
                        
                        throw new NotImplementedException (); 
                }