[winforms] Style
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
index 73e4ada2afed12052166b32134be301ade973c94..d88c3e129f51b24ae3f22a485c32b3e5d8cdaf91 100644 (file)
@@ -51,17 +51,39 @@ namespace System.Drawing
        {
                #region constructors
                // constructors
+
+#if NET_2_0
+               // required for XmlSerializer (#323246)
+               private Bitmap ()
+               {
+               }
+#endif
+
                internal Bitmap (IntPtr ptr)
                {
                        nativeObject = ptr;
                }
 
+               // Usually called when cloning images that need to have
+               // not only the handle saved, but also the underlying stream
+               // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
+               internal Bitmap(IntPtr ptr, Stream stream)
+               {
+                       // under Win32 stream is owned by SD/GDI+ code
+                       if (GDIPlus.RunningOnWindows ())
+                               this.stream = stream;
+                       nativeObject = ptr;
+               }
+
                public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
                {
                }
 
                public Bitmap (int width, int height, Graphics g)
-               {               
+               {
+                       if (g == null)
+                               throw new ArgumentNullException ("g");
+
                        IntPtr bmp;
                        Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
                        GDIPlus.CheckStatus (s);
@@ -93,6 +115,9 @@ namespace System.Drawing
 
                public Bitmap (string filename, bool useIcm)
                {
+                       if (filename == null)
+                               throw new ArgumentNullException ("filename");
+
                        IntPtr imagePtr;
                        Status st;
 
@@ -107,6 +132,9 @@ namespace System.Drawing
 
                public Bitmap (Type type, string resource)
                {
+                       if (resource == null)
+                               throw new ArgumentException ("resource");
+
                        Stream s = type.Assembly.GetManifestResourceStream (type, resource);
                        if (s == null) {
                                string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
@@ -154,8 +182,20 @@ 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);
                }
 
@@ -275,9 +315,9 @@ namespace System.Drawing
                        GDIPlus.CheckStatus (status);
                }
 
-               public void UnlockBits (BitmapData bitmap_data)
+               public void UnlockBits (BitmapData bitmapdata)
                {
-                       Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
+                       Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmapdata);
                        GDIPlus.CheckStatus (status);
                }
        }