2008-05-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
index f05b555c2928b5779e76701755c8d4e7cc0ab76b..799ad32daa643df1f872d1a2f0cfaef112c54382 100644 (file)
@@ -51,6 +51,14 @@ namespace System.Drawing
        {
                #region constructors
                // constructors
+
+#if NET_2_0
+               // required for XmlSerializer (#323246)
+               private Bitmap ()
+               {
+               }
+#endif
+
                internal Bitmap (IntPtr ptr)
                {
                        nativeObject = ptr;
@@ -58,7 +66,6 @@ namespace System.Drawing
 
                public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
                {
-                       
                }
 
                public Bitmap (int width, int height, Graphics g)
@@ -86,23 +93,10 @@ namespace System.Drawing
 
                public Bitmap (Image original, Size newSize)  : this(original, newSize.Width, newSize.Height) {}
                
-               internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
-               {                       
-                       nativeObject = bmp;                                             
-               }
-               
-               internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
-               {                       
-                       nativeObject = bmp;                     
-                       
-               }
-
                public Bitmap (Stream stream, bool useIcm)
                {
-                       if (stream == null)
-                               throw new ArgumentNullException ("stream");
-
-                       InitFromStream (stream);
+                       // false: stream is owned by user code
+                       nativeObject = InitFromStream (stream);
                }
 
                public Bitmap (string filename, bool useIcm)
@@ -121,12 +115,16 @@ namespace System.Drawing
 
                public Bitmap (Type type, string resource)
                {
-                       using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)){
-                               if (s == null)
-                                       throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
-
-                               InitFromStream (s);
+                       Stream s = type.Assembly.GetManifestResourceStream (type, resource);
+                       if (s == null) {
+                               string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
+                               throw new FileNotFoundException (msg);
                        }
+
+                       nativeObject = InitFromStream (s);
+                       // under Win32 stream is owned by SD/GDI+ code
+                       if (GDIPlus.RunningOnWindows ())
+                               stream = s;
                }
 
                public Bitmap (Image original, int width, int height)  : this(width, height, PixelFormat.Format32bppArgb)
@@ -147,23 +145,9 @@ namespace System.Drawing
                }
 
                private Bitmap (SerializationInfo info, StreamingContext context)
+                       : base (info, context)
                {
-                       foreach (SerializationEntry serEnum in info) {
-                               if (String.Compare(serEnum.Name, "Data", true) == 0) {
-                                       byte[] bytes = (byte[]) serEnum.Value;
-               
-                                       if (bytes != null) {
-                                               InitFromStream(new MemoryStream(bytes));
-                                       }
-                               }
-                       }
                }
-               //The below function is not required. Call should resolve to base
-               //Moreover there is a problem with the declaration. Base class function
-               //is not declared as protected to access in descendent class
-               /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
-               {
-               }*/
 
                #endregion
                // methods
@@ -178,52 +162,55 @@ namespace System.Drawing
                }
 
                public void SetPixel (int x, int y, Color color)
-               {                                                                       
-                       Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
+               {
+                       Status s = GDIPlus.GdipBitmapSetPixel (nativeObject, x, y, color.ToArgb ());
+                       if (s == Status.InvalidParameter) {
+                               // check is done in case of an error only to avoid another
+                               // unmanaged call for normal (successful) calls
+                               if ((this.PixelFormat & PixelFormat.Indexed) != 0) {
+                                       string msg = Locale.GetText ("SetPixel cannot be called on indexed bitmaps.");
+#if NET_2_0
+                                       throw new InvalidOperationException (msg);
+#else
+                                       throw new Exception (msg);
+#endif
+                               }
+                       }
                        GDIPlus.CheckStatus (s);
                }
 
-               public Bitmap Clone (Rectangle rect,PixelFormat format)
+               public Bitmap Clone (Rectangle rect, PixelFormat format)
                {                               
                        IntPtr bmp;                     
-                       Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
-                               format, nativeObject,  out bmp);
-                               
+                       Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
+                               format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
-
-                       Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  format, bmp);
-                               return bmpnew;
+                       return new Bitmap (bmp);
                        }
                
                public Bitmap Clone (RectangleF rect, PixelFormat format)
                {
                        IntPtr bmp;                     
-                       Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
-                               format, nativeObject,  out bmp);
+                       Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
+                               format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
-
-                       Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  format, bmp);
-                       return bmpnew;
+                       return new Bitmap (bmp);
                }
 
-               public static Bitmap FromHicon (IntPtr hicon)   //TODO: Untested
+               public static Bitmap FromHicon (IntPtr hicon)
                {       
                        IntPtr bitmap;  
-                               
                        Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
                        GDIPlus.CheckStatus (status);
-
-                       return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap);   // FIXME
+                       return new Bitmap (bitmap);
                }
 
                public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
                {
                        IntPtr bitmap;  
-                               
                        Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
                        GDIPlus.CheckStatus (status);
-
-                       return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
+                       return new Bitmap (bitmap);
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
@@ -268,16 +255,7 @@ namespace System.Drawing
 #endif
                BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
                {
-                       if (bitmapData == null)
-                               throw new ArgumentException ("bitmapData");
-
-                       int img_format = (int) PixelFormat;
-                       if ((int) format != img_format && (img_format & (int) PixelFormat.Indexed) != 0 &&
-                           (flags == ImageLockMode.WriteOnly || flags == ImageLockMode.ReadWrite)) {
-                               throw new ArgumentException ("Parameter is not valid.");
-                       }
-                               
-                       Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format,  bitmapData);
+                       Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
                        //NOTE: scan0 points to piece of memory allocated in the unmanaged space
                        GDIPlus.CheckStatus (status);