[winforms] Style
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
index e6fdab42270f72e0acd34345c80edae987fa8fdc..d88c3e129f51b24ae3f22a485c32b3e5d8cdaf91 100644 (file)
@@ -64,20 +64,34 @@ namespace System.Drawing
                        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);
-                       nativeObject = bmp;
+                       nativeObject = bmp;                                             
                }
 
                public Bitmap (int width, int height, PixelFormat format)
-               {
+               {       
                        IntPtr bmp;
                        Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
                        GDIPlus.CheckStatus (s);
@@ -101,6 +115,9 @@ namespace System.Drawing
 
                public Bitmap (string filename, bool useIcm)
                {
+                       if (filename == null)
+                               throw new ArgumentNullException ("filename");
+
                        IntPtr imagePtr;
                        Status st;
 
@@ -115,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);
@@ -136,12 +156,12 @@ namespace System.Drawing
                }
 
                public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
-               {
+               {               
                        IntPtr bmp;
-
+                               
                        Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
-                       GDIPlus.CheckStatus (status);
-                       nativeObject = bmp;
+                       GDIPlus.CheckStatus (status);   
+                       nativeObject = bmp;                                                                                                             
                }
 
                private Bitmap (SerializationInfo info, StreamingContext context)
@@ -153,12 +173,12 @@ namespace System.Drawing
                // methods
                public Color GetPixel (int x, int y) {
                        
-                       int argb;
-
+                       int argb;                               
+                       
                        Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
                        GDIPlus.CheckStatus (s);
 
-                       return Color.FromArgb(argb);
+                       return Color.FromArgb(argb);            
                }
 
                public void SetPixel (int x, int y, Color color)
@@ -180,17 +200,17 @@ namespace System.Drawing
                }
 
                public Bitmap Clone (Rectangle rect, PixelFormat format)
-               {
-                       IntPtr bmp;
+               {                               
+                       IntPtr bmp;                     
                        Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
                                format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
                        return new Bitmap (bmp);
-               }
+                       }
                
                public Bitmap Clone (RectangleF rect, PixelFormat format)
                {
-                       IntPtr bmp;
+                       IntPtr bmp;                     
                        Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
                                format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
@@ -199,7 +219,7 @@ namespace System.Drawing
 
                public static Bitmap FromHicon (IntPtr hicon)
                {       
-                       IntPtr bitmap;
+                       IntPtr bitmap;  
                        Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
                        GDIPlus.CheckStatus (status);
                        return new Bitmap (bitmap);
@@ -207,7 +227,7 @@ namespace System.Drawing
 
                public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
                {
-                       IntPtr bitmap;
+                       IntPtr bitmap;  
                        Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
                        GDIPlus.CheckStatus (status);
                        return new Bitmap (bitmap);
@@ -241,7 +261,7 @@ namespace System.Drawing
                        Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
                        GDIPlus.CheckStatus (status);
 
-                       return  HandleIcon;
+                       return  HandleIcon;                     
                }
 
                public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
@@ -264,12 +284,12 @@ namespace System.Drawing
 
                public void MakeTransparent ()
                {
-                       Color clr = GetPixel(0,0);
+                       Color clr = GetPixel(0,0);                      
                        MakeTransparent (clr);
                }
 
                public void MakeTransparent (Color transparentColor)
-               {
+               {                                                       
                        // We have to draw always over a 32-bitmap surface that supports alpha channel
                        Bitmap  bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
                        Graphics gr = Graphics.FromImage(bmp);
@@ -278,7 +298,7 @@ namespace System.Drawing
                        
                        imageAttr.SetColorKey(transparentColor, transparentColor);
 
-                       gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
+                       gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);                                      
                        
                        IntPtr oldBmp = nativeObject;
                        nativeObject = bmp.nativeObject;