* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / Graphics.cs
index c90c0ab32875b52df067228b67d8d8ea51643245..a5a7c0984f1445f1c33bcd56794c56956dfd96bb 100644 (file)
@@ -40,25 +40,37 @@ using System.Text;
 
 namespace System.Drawing
 {
-       [ComVisible(false)]
+#if !NET_2_0
+       [ComVisible(false)]  
+#endif
        public sealed class Graphics : MarshalByRefObject, IDisposable
+#if NET_2_0
+       , IDeviceContext
+#endif
        {
                internal IntPtr nativeObject = IntPtr.Zero;
                private bool disposed = false;
                private static float defDpiX = 0;
                private static float defDpiY = 0;
+#if NET_2_0
+               private IntPtr deviceContextHdc;
+#endif
 
+#if !NET_2_0
                [ComVisible(false)]
+#endif
                public delegate bool EnumerateMetafileProc (EmfPlusRecordType recordType,
                                                            int flags,
                                                            int dataSize,
                                                            IntPtr data,
                                                            PlayRecordCallback callbackData);
                
+#if !NET_2_0
                [ComVisible (false)]
+#endif
                public delegate bool DrawImageAbort (IntPtr callbackData);
 
-               private Graphics (IntPtr nativeGraphics)
+               internal Graphics (IntPtr nativeGraphics)
                {
                        nativeObject = nativeGraphics;
                }
@@ -74,6 +86,7 @@ namespace System.Drawing
                                        Bitmap bmp = new Bitmap (1, 1);
                                        Graphics g = Graphics.FromImage (bmp);
                                        defDpiX = g.DpiX;
+                                       defDpiY = g.DpiY;
                                }
                                return defDpiX;
                        }
@@ -84,6 +97,7 @@ namespace System.Drawing
                                if (defDpiY == 0) {
                                        Bitmap bmp = new Bitmap (1, 1);
                                        Graphics g = Graphics.FromImage (bmp);
+                                       defDpiX = g.DpiX;
                                        defDpiY = g.DpiY;
                                }
                                return defDpiY;
@@ -99,40 +113,40 @@ namespace System.Drawing
                                nativeObject = value;
                        }
                }
-
+               
                [MonoTODO]
                public void AddMetafileComment (byte [] data)
                {
                        throw new NotImplementedException ();
                }
 
-               
                public GraphicsContainer BeginContainer ()
                {
-                       int state;
+                       uint state;
                        Status status;
                        status = GDIPlus.GdipBeginContainer2 (nativeObject, out state);
                        GDIPlus.CheckStatus (status);
 
                         return new GraphicsContainer(state);
                }
-               
+
+               [MonoTODO ("rectangles and unit aren't supported in libgdiplus")]               
                public GraphicsContainer BeginContainer (Rectangle dstrect, Rectangle srcrect, GraphicsUnit unit)
                {
-                       int state;
+                       uint state;
                        Status status;
-                       status = GDIPlus.GdipBeginContainerI (nativeObject, dstrect, srcrect, unit, out state);
+                       status = GDIPlus.GdipBeginContainerI (nativeObject, ref dstrect, ref srcrect, unit, out state);
                        GDIPlus.CheckStatus (status);
 
                        return new GraphicsContainer (state);
                }
 
-               
+               [MonoTODO ("rectangles and unit aren't supported in libgdiplus")]               
                public GraphicsContainer BeginContainer (RectangleF dstrect, RectangleF srcrect, GraphicsUnit unit)
                {
-                       int state;
+                       uint state;
                        Status status;
-                       status = GDIPlus.GdipBeginContainer (nativeObject, dstrect, srcrect, unit, out state);
+                       status = GDIPlus.GdipBeginContainer (nativeObject, ref dstrect, ref srcrect, unit, out state);
                        GDIPlus.CheckStatus (status);
 
                        return new GraphicsContainer (state);
@@ -145,6 +159,104 @@ namespace System.Drawing
                        status = GDIPlus.GdipGraphicsClear (nativeObject, color.ToArgb ());
                        GDIPlus.CheckStatus (status);
                }
+#if NET_2_0            
+               public void CopyFromScreen (Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize)
+               {
+                       CopyFromScreen (upperLeftSource.X, upperLeftSource.Y, upperLeftDestination.X, upperLeftDestination.Y,
+                               blockRegionSize, CopyPixelOperation.SourceCopy);                                
+               }
+
+               public void CopyFromScreen (Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
+               {
+                       CopyFromScreen (upperLeftSource.X, upperLeftSource.Y, upperLeftDestination.X, upperLeftDestination.Y,
+                               blockRegionSize, copyPixelOperation);
+               }\r
+               
+               public void CopyFromScreen (int sourceX, int sourceY, int destinationX, int destinationY, Size blockRegionSize)
+               {
+                       CopyFromScreen (sourceX, sourceY, destinationX, destinationY, blockRegionSize,
+                               CopyPixelOperation.SourceCopy);
+               }
+
+               public void CopyFromScreen (int sourceX, int sourceY, int destinationX, int destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)\r
+               {
+                       IntPtr window;                  
+
+                       if (!Enum.IsDefined (typeof (CopyPixelOperation), copyPixelOperation))
+                               throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CopyPixelOperation", copyPixelOperation));
+
+                       if (GDIPlus.UseCocoaDrawable || GDIPlus.UseQuartzDrawable) {
+                               throw new NotImplementedException ();
+                       }
+                       
+                       if (GDIPlus.UseX11Drawable) { // X11 implementation
+                               IntPtr image, defvisual, vPtr;
+                               int AllPlanes = ~0, nitems = 0, pixel;
+
+                               if (copyPixelOperation != CopyPixelOperation.SourceCopy)
+                                       throw new NotImplementedException ("Operation not implemented under X11");
+               
+                               if (GDIPlus.Display == IntPtr.Zero) {
+                                       GDIPlus.Display = GDIPlus.XOpenDisplay (IntPtr.Zero);                                   
+                               }
+
+                               window = GDIPlus.XRootWindow (GDIPlus.Display, 0);
+                               defvisual = GDIPlus.XDefaultVisual (GDIPlus.Display, 0);                                
+                               XVisualInfo visual = new XVisualInfo ();
+
+                               /* Get XVisualInfo for this visual */
+                               visual.visualid = GDIPlus.XVisualIDFromVisual(defvisual);
+                               vPtr = GDIPlus.XGetVisualInfo (GDIPlus.Display, 0x1 /* VisualIDMask */, ref visual, ref nitems);
+                               visual = (XVisualInfo) Marshal.PtrToStructure(vPtr, typeof (XVisualInfo));
+
+                               /* Sorry I do not have access to a computer with > deepth. Fell free to add more pixel formats */       
+                               image = GDIPlus.XGetImage (GDIPlus.Display, window, sourceX, sourceY, blockRegionSize.Width,
+                                       blockRegionSize.Height, AllPlanes, 2 /* ZPixmap*/);
+                               
+                               Bitmap bmp = new Bitmap (blockRegionSize.Width, blockRegionSize.Height);
+                               int red, blue, green;
+                               for (int y = sourceY; y <  sourceY + blockRegionSize.Height; y++) {
+                                       for (int x = sourceX; x <  sourceX + blockRegionSize.Width; x++) {
+                                               pixel = GDIPlus.XGetPixel (image, x, y);                        
+
+                                               switch (visual.depth) {
+                                                       case 16: /* 16bbp pixel transformation */
+                                                               red = (int) ((pixel & visual.red_mask ) >> 8) & 0xff;
+                                                               green = (int) (((pixel & visual.green_mask ) >> 3 )) & 0xff;
+                                                               blue = (int) ((pixel & visual.blue_mask ) << 3 ) & 0xff;
+                                                               break;
+                                                       case 24:
+                                                       case 32:
+                                                               red = (int) ((pixel & visual.red_mask ) >> 16) & 0xff;
+                                                               green = (int) (((pixel & visual.green_mask ) >> 8 )) & 0xff;
+                                                               blue = (int) ((pixel & visual.blue_mask )) & 0xff;
+                                                               break;
+                                                       default:
+                                                               throw new NotImplementedException ("Deepth not supported right now");
+                                               }
+                                               
+                                               bmp.SetPixel (x, y, Color.FromArgb (255, red, green, blue));                                                     
+                                       }
+                               }
+
+                               DrawImage (bmp, 0, 0);
+                               bmp.Dispose ();
+                               GDIPlus.XDestroyImage (image);
+                               GDIPlus.XFree (vPtr);
+                               return;
+                       }                       
+
+                       // Win32 implementation
+                       window = GDIPlus.GetDesktopWindow ();
+                       IntPtr srcDC = GDIPlus.GetDC (window);
+                       IntPtr dstDC = GetHdc ();
+                       GDIPlus.BitBlt (dstDC, destinationX, destinationY, blockRegionSize.Width,
+                               blockRegionSize.Height, srcDC, sourceX, sourceY, (int) copyPixelOperation);
+
+                       GDIPlus.ReleaseDC (srcDC);
+                       ReleaseHdc (dstDC);                     
+               }\r
+#endif
 
                public void Dispose ()
                {
@@ -155,6 +267,7 @@ namespace System.Drawing
                                GDIPlus.CheckStatus (status);
                                disposed = true;                                
                        }
+                       GC.SuppressFinalize(this);
                }
 
                
@@ -173,6 +286,9 @@ namespace System.Drawing
                public void DrawArc (Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
                {
                        Status status;
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       
                        status = GDIPlus.GdipDrawArc (nativeObject, pen.nativeObject,
                                         x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
@@ -184,6 +300,8 @@ namespace System.Drawing
                public void DrawArc (Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
                {
                        Status status;
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        status = GDIPlus.GdipDrawArcI (nativeObject, pen.nativeObject,
                                                x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
@@ -192,6 +310,8 @@ namespace System.Drawing
                public void DrawBezier (Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4)
                {
                        Status status;
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        status = GDIPlus.GdipDrawBezier (nativeObject, pen.nativeObject,
                                                        pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X,
                                                        pt3.Y, pt4.X, pt4.Y);
@@ -201,6 +321,8 @@ namespace System.Drawing
                public void DrawBezier (Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)
                {
                        Status status;
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        status = GDIPlus.GdipDrawBezierI (nativeObject, pen.nativeObject,
                                                        pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X,
                                                        pt3.Y, pt4.X, pt4.Y);
@@ -210,6 +332,8 @@ namespace System.Drawing
                public void DrawBezier (Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
                {
                        Status status;
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        status = GDIPlus.GdipDrawBezier (nativeObject, pen.nativeObject, x1,
                                                        y1, x2, y2, x3, y3, x4, y4);
                        GDIPlus.CheckStatus (status);
@@ -217,13 +341,18 @@ namespace System.Drawing
 
                public void DrawBeziers (Pen pen, Point [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                         int length = points.Length;
                        Status status;
 
-                        if (length < 3)
+                        if (length < 4)
                                 return;
 
-                       for (int i = 0; i < length; i += 3) {
+                       for (int i = 0; i < length - 1; i += 3) {
                                 Point p1 = points [i];
                                 Point p2 = points [i + 1];
                                 Point p3 = points [i + 2];
@@ -239,13 +368,18 @@ namespace System.Drawing
 
                public void DrawBeziers (Pen pen, PointF [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        int length = points.Length;
                        Status status;
 
-                        if (length < 3)
+                        if (length < 4)
                                 return;
 
-                       for (int i = 0; i < length; i += 3) {
+                       for (int i = 0; i < length - 1; i += 3) {
                                 PointF p1 = points [i];
                                 PointF p2 = points [i + 1];
                                 PointF p3 = points [i + 2];
@@ -262,6 +396,11 @@ namespace System.Drawing
                
                public void DrawClosedCurve (Pen pen, PointF [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawClosedCurve (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
@@ -269,6 +408,11 @@ namespace System.Drawing
                
                public void DrawClosedCurve (Pen pen, Point [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawClosedCurveI (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
@@ -276,6 +420,11 @@ namespace System.Drawing
                        
                public void DrawClosedCurve (Pen pen, Point [] points, float tension, FillMode fillmode)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawClosedCurve2I (nativeObject, pen.nativeObject, points, points.Length, tension);
                        GDIPlus.CheckStatus (status);
@@ -283,6 +432,11 @@ namespace System.Drawing
                
                public void DrawClosedCurve (Pen pen, PointF [] points, float tension, FillMode fillmode)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawClosedCurve2 (nativeObject, pen.nativeObject, points, points.Length, tension);
                        GDIPlus.CheckStatus (status);
@@ -290,6 +444,11 @@ namespace System.Drawing
                
                public void DrawCurve (Pen pen, Point [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurveI (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
@@ -297,6 +456,11 @@ namespace System.Drawing
                
                public void DrawCurve (Pen pen, PointF [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
@@ -304,6 +468,11 @@ namespace System.Drawing
                
                public void DrawCurve (Pen pen, PointF [] points, float tension)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve2 (nativeObject, pen.nativeObject, points, points.Length, tension);
                        GDIPlus.CheckStatus (status);
@@ -311,14 +480,23 @@ namespace System.Drawing
                
                public void DrawCurve (Pen pen, Point [] points, float tension)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve2I (nativeObject, pen.nativeObject, points, points.Length, tension);              
                        GDIPlus.CheckStatus (status);
                }
                
-               
                public void DrawCurve (Pen pen, PointF [] points, int offset, int numberOfSegments)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve3 (nativeObject, pen.nativeObject,
                                                        points, points.Length, offset,
@@ -328,6 +506,11 @@ namespace System.Drawing
 
                public void DrawCurve (Pen pen, Point [] points, int offset, int numberOfSegments, float tension)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve3I (nativeObject, pen.nativeObject,
                                                        points, points.Length, offset,
@@ -335,9 +518,13 @@ namespace System.Drawing
                        GDIPlus.CheckStatus (status);
                }
 
-               
                public void DrawCurve (Pen pen, PointF [] points, int offset, int numberOfSegments, float tension)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       
                        Status status;
                        status = GDIPlus.GdipDrawCurve3 (nativeObject, pen.nativeObject,
                                                        points, points.Length, offset,
@@ -347,16 +534,23 @@ namespace System.Drawing
 
                public void DrawEllipse (Pen pen, Rectangle rect)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       
                        DrawEllipse (pen, rect.X, rect.Y, rect.Width, rect.Height);
                }
 
                public void DrawEllipse (Pen pen, RectangleF rect)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        DrawEllipse (pen, rect.X, rect.Y, rect.Width, rect.Height);
                }
 
                public void DrawEllipse (Pen pen, int x, int y, int width, int height)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status;
                        status = GDIPlus.GdipDrawEllipseI (nativeObject, pen.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
@@ -364,30 +558,38 @@ namespace System.Drawing
 
                public void DrawEllipse (Pen pen, float x, float y, float width, float height)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawEllipse (nativeObject, pen.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawIcon (Icon icon, Rectangle targetRect)
                {
-                       Image img = icon.ToBitmap ();
-                       DrawImage (img, targetRect);
+                       using (Image img = icon.ToBitmap ()) {
+                               DrawImage (img, targetRect);
+                       }
                }
 
                public void DrawIcon (Icon icon, int x, int y)
                {
-                       Image img = icon.ToBitmap ();
-                       DrawImage (img, x, y);
+                       using (Image img = icon.ToBitmap ()) {
+                               DrawImage (img, x, y);
+                       }
                }
 
                public void DrawIconUnstretched (Icon icon, Rectangle targetRect)
                {
-                       Image img = icon.ToBitmap ();
-                       DrawImageUnscaled (img, targetRect);
+                       using (Image img = icon.ToBitmap ()) {
+                               DrawImageUnscaled (img, targetRect);
+                       }
                }
                
                public void DrawImage (Image image, RectangleF rect)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       
                        Status status = GDIPlus.GdipDrawImageRect(nativeObject, image.NativeObject, rect.X, rect.Y, rect.Width, rect.Height);
                        GDIPlus.CheckStatus (status);
                }
@@ -395,6 +597,9 @@ namespace System.Drawing
                
                public void DrawImage (Image image, PointF point)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       
                        Status status = GDIPlus.GdipDrawImage (nativeObject, image.NativeObject, point.X, point.Y);
                        GDIPlus.CheckStatus (status);
                }
@@ -402,6 +607,11 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Point [] destPoints)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
+                       
                        Status status = GDIPlus.GdipDrawImagePointsI (nativeObject, image.NativeObject, destPoints, destPoints.Length);
                        GDIPlus.CheckStatus (status);
                }
@@ -409,18 +619,26 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Point point)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        DrawImage (image, point.X, point.Y);
                }
 
                
                public void DrawImage (Image image, Rectangle rect)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        DrawImage (image, rect.X, rect.Y, rect.Width, rect.Height);
                }
 
                
                public void DrawImage (Image image, PointF [] destPoints)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        Status status = GDIPlus.GdipDrawImagePoints (nativeObject, image.NativeObject, destPoints, destPoints.Length);
                        GDIPlus.CheckStatus (status);
                }
@@ -428,6 +646,8 @@ namespace System.Drawing
                
                public void DrawImage (Image image, int x, int y)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageI (nativeObject, image.NativeObject, x, y);
                        GDIPlus.CheckStatus (status);
                }
@@ -435,6 +655,8 @@ namespace System.Drawing
                
                public void DrawImage (Image image, float x, float y)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImage (nativeObject, image.NativeObject, x, y);
                        GDIPlus.CheckStatus (status);
                }
@@ -442,6 +664,8 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRectI (nativeObject, image.NativeObject,
                                destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height,
@@ -451,6 +675,8 @@ namespace System.Drawing
                
                public void DrawImage (Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit)
                {                       
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject,
                                destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height,
@@ -461,6 +687,11 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Point [] destPoints, Rectangle srcRect, GraphicsUnit srcUnit)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
+                       
                        Status status = GDIPlus.GdipDrawImagePointsRectI (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y, 
                                srcRect.Width, srcRect.Height, srcUnit, IntPtr.Zero, 
@@ -471,6 +702,10 @@ namespace System.Drawing
                
                public void DrawImage (Image image, PointF [] destPoints, RectangleF srcRect, GraphicsUnit srcUnit)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        
                        Status status = GDIPlus.GdipDrawImagePointsRect (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y, 
@@ -483,15 +718,21 @@ namespace System.Drawing
                public void DrawImage (Image image, Point [] destPoints, Rectangle srcRect, GraphicsUnit srcUnit, 
                                 ImageAttributes imageAttr)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        Status status = GDIPlus.GdipDrawImagePointsRectI (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y,
                                srcRect.Width, srcRect.Height, srcUnit,
-                               imageAttr.NativeObject, null, IntPtr.Zero);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, null, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawImage (Image image, float x, float y, float width, float height)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRect(nativeObject, image.NativeObject, x, y,
                            width, height);
                        GDIPlus.CheckStatus (status);
@@ -501,28 +742,38 @@ namespace System.Drawing
                public void DrawImage (Image image, PointF [] destPoints, RectangleF srcRect, GraphicsUnit srcUnit, 
                                 ImageAttributes imageAttr)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        Status status = GDIPlus.GdipDrawImagePointsRect (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y,
                                srcRect.Width, srcRect.Height, srcUnit, 
-                               imageAttr.NativeObject, null, IntPtr.Zero);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, null, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, int x, int y, Rectangle srcRect, GraphicsUnit srcUnit)
                {                       
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImagePointRectI(nativeObject, image.NativeObject, x, y, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, srcUnit);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawImage (Image image, int x, int y, int width, int height)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectI (nativeObject, image.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawImage (Image image, float x, float y, RectangleF srcRect, GraphicsUnit srcUnit)
                {                       
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImagePointRect (nativeObject, image.nativeObject, x, y, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, srcUnit);
                        GDIPlus.CheckStatus (status);
                }
@@ -530,38 +781,52 @@ namespace System.Drawing
                
                public void DrawImage (Image image, PointF [] destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        Status status = GDIPlus.GdipDrawImagePointsRect (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y,
                                srcRect.Width, srcRect.Height, srcUnit, 
-                               imageAttr.NativeObject, callback, IntPtr.Zero);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, Point [] destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
                        
                        Status status = GDIPlus.GdipDrawImagePointsRectI (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y,
                                srcRect.Width, srcRect.Height, srcUnit, 
-                               imageAttr.NativeObject, callback, IntPtr.Zero);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, Point [] destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback, int callbackData)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
+                       if (destPoints == null)
+                               throw new ArgumentNullException ("destPoints");
 
                        Status status = GDIPlus.GdipDrawImagePointsRectI (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y, 
                                srcRect.Width, srcRect.Height, srcUnit, 
-                               imageAttr.NativeObject, callback, (IntPtr) callbackData);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, (IntPtr) callbackData);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject,
                                 destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                        srcX, srcY, srcWidth, srcHeight, srcUnit, IntPtr.Zero, 
@@ -574,13 +839,15 @@ namespace System.Drawing
                        Status status = GDIPlus.GdipDrawImagePointsRect (nativeObject, image.NativeObject,
                                destPoints, destPoints.Length , srcRect.X, srcRect.Y,
                                srcRect.Width, srcRect.Height, srcUnit, 
-                               imageAttr.NativeObject, callback, (IntPtr) callbackData);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, (IntPtr) callbackData);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRectI (nativeObject, image.NativeObject,
                                 destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                        srcX, srcY, srcWidth, srcHeight, srcUnit, IntPtr.Zero, 
@@ -591,38 +858,46 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject,
                                 destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                        srcX, srcY, srcWidth, srcHeight, srcUnit,
-                               imageAttrs.NativeObject, null, IntPtr.Zero);
+                               imageAttrs != null ? imageAttrs.NativeObject : IntPtr.Zero, null, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawImage (Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)
                {                       
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRectI (nativeObject, image.NativeObject, 
                                         destRect.X, destRect.Y, destRect.Width, 
                                        destRect.Height, srcX, srcY, srcWidth, srcHeight,
-                                       srcUnit, imageAttr.NativeObject, null, IntPtr.Zero);
+                                       srcUnit, imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, null, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawImage (Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRectI (nativeObject, image.NativeObject, 
                                         destRect.X, destRect.Y, destRect.Width, 
                                        destRect.Height, srcX, srcY, srcWidth, srcHeight,
-                                       srcUnit, imageAttr.NativeObject, callback,
+                                       srcUnit, imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback,
                                        IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawImage (Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject, 
                                         destRect.X, destRect.Y, destRect.Width, 
                                        destRect.Height, srcX, srcY, srcWidth, srcHeight,
-                                       srcUnit, imageAttr.NativeObject
+                                       srcUnit, imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero
                                        callback, IntPtr.Zero);
                        GDIPlus.CheckStatus (status);
                }
@@ -630,40 +905,52 @@ namespace System.Drawing
                
                public void DrawImage (Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback, IntPtr callbackData)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject, 
                                destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                srcX, srcY, srcWidth, srcHeight, srcUnit, 
-                               imageAttr.NativeObject, callback, callbackData);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, callbackData);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void DrawImage (Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback, IntPtr callbackData)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Status status = GDIPlus.GdipDrawImageRectRect (nativeObject, image.NativeObject, 
                                        destRect.X, destRect.Y, destRect.Width, destRect.Height,
                                srcX, srcY, srcWidth, srcHeight, srcUnit,
-                               imageAttr.NativeObject, callback, callbackData);
+                               imageAttr != null ? imageAttr.NativeObject : IntPtr.Zero, callback, callbackData);
                        GDIPlus.CheckStatus (status);
                }               
                
                public void DrawImageUnscaled (Image image, Point point)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        DrawImageUnscaled (image, point.X, point.Y);
                }
                
                public void DrawImageUnscaled (Image image, Rectangle rect)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        DrawImageUnscaled (image, rect.X, rect.Y, rect.Width, rect.Height);
                }
                
                public void DrawImageUnscaled (Image image, int x, int y)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        DrawImage (image, x, y, image.Width, image.Height);
                }
 
                public void DrawImageUnscaled (Image image, int x, int y, int width, int height)
                {
+                       if (image == null)
+                               throw new ArgumentNullException ("image");
                        Image tmpImg = new Bitmap (width, height);
                        Graphics g = FromImage (tmpImg);
                        g.DrawImage (image, 0, 0, image.Width, image.Height);
@@ -672,8 +959,21 @@ namespace System.Drawing
                        g.Dispose ();
                }
 
+#if NET_2_0
+               public void DrawImageUnscaledAndClipped (Image image, Rectangle rect)
+               {
+                       int height, width;                      
+                       width = (image.Width > rect.Width) ? rect.Width : image.Width;
+                       height = (image.Height > rect.Height) ? rect.Height : image.Height;
+
+                       DrawImageUnscaled (image, rect.X, rect.Y, width, height);                       
+               }\r
+#endif
+
                public void DrawLine (Pen pen, PointF pt1, PointF pt2)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                         Status status = GDIPlus.GdipDrawLine (nativeObject, pen.nativeObject,
                                                pt1.X, pt1.Y, pt2.X, pt2.Y);
                        GDIPlus.CheckStatus (status);
@@ -681,6 +981,8 @@ namespace System.Drawing
 
                public void DrawLine (Pen pen, Point pt1, Point pt2)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                         Status status = GDIPlus.GdipDrawLineI (nativeObject, pen.nativeObject,
                                                pt1.X, pt1.Y, pt2.X, pt2.Y);
                        GDIPlus.CheckStatus (status);
@@ -688,46 +990,68 @@ namespace System.Drawing
 
                public void DrawLine (Pen pen, int x1, int y1, int x2, int y2)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawLineI (nativeObject, pen.nativeObject, x1, y1, x2, y2);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawLine (Pen pen, float x1, float y1, float x2, float y2)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawLine (nativeObject, pen.nativeObject, x1, y1, x2, y2);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawLines (Pen pen, PointF [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipDrawLines (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawLines (Pen pen, Point [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipDrawLinesI (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawPath (Pen pen, GraphicsPath path)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
                        Status status = GDIPlus.GdipDrawPath (nativeObject, pen.nativeObject, path.nativePath);
                        GDIPlus.CheckStatus (status);
                }
                
                public void DrawPie (Pen pen, Rectangle rect, float startAngle, float sweepAngle)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        DrawPie (pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
                }
                
                public void DrawPie (Pen pen, RectangleF rect, float startAngle, float sweepAngle)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        DrawPie (pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
                }
                
                public void DrawPie (Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawPie (nativeObject, pen.nativeObject, x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
                }
@@ -737,52 +1061,78 @@ namespace System.Drawing
                // int sweepAngle. However, GdipDrawPieI uses also float for the startAngle and sweepAngle params
                public void DrawPie (Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawPieI (nativeObject, pen.nativeObject, x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawPolygon (Pen pen, Point [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipDrawPolygonI (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawPolygon (Pen pen, PointF [] points)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipDrawPolygon (nativeObject, pen.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                internal void DrawRectangle (Pen pen, RectangleF rect)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        DrawRectangle (pen, rect.Left, rect.Top, rect.Width, rect.Height);
                }
 
                public void DrawRectangle (Pen pen, Rectangle rect)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        DrawRectangle (pen, rect.Left, rect.Top, rect.Width, rect.Height);
                }
 
                public void DrawRectangle (Pen pen, float x, float y, float width, float height)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawRectangle (nativeObject, pen.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawRectangle (Pen pen, int x, int y, int width, int height)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("pen");
                        Status status = GDIPlus.GdipDrawRectangleI (nativeObject, pen.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawRectangles (Pen pen, RectangleF [] rects)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("image");
+                       if (rects == null)
+                               throw new ArgumentNullException ("rects");
                        Status status = GDIPlus.GdipDrawRectangles (nativeObject, pen.nativeObject, rects, rects.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void DrawRectangles (Pen pen, Rectangle [] rects)
                {
+                       if (pen == null)
+                               throw new ArgumentNullException ("image");
+                       if (rects == null)
+                               throw new ArgumentNullException ("rects");
                        Status status = GDIPlus.GdipDrawRectanglesI (nativeObject, pen.nativeObject, rects, rects.Length);
                        GDIPlus.CheckStatus (status);
                }
@@ -827,6 +1177,10 @@ namespace System.Drawing
 
                public void EndContainer (GraphicsContainer container)
                {
+#if NET_2_0
+                       if (container == null)
+                               throw new ArgumentNullException ("container");
+#endif
                        Status status = GDIPlus.GdipEndContainer(nativeObject, container.NativeObject);
                        GDIPlus.CheckStatus (status);
                }
@@ -1055,6 +1409,8 @@ namespace System.Drawing
 
                public void ExcludeClip (Region region)
                {
+                       if (region == null)
+                               throw new ArgumentNullException ("region");
                        Status status = GDIPlus.GdipSetClipRegion (nativeObject, region.NativeObject, CombineMode.Exclude);
                        GDIPlus.CheckStatus (status);
                }
@@ -1062,13 +1418,21 @@ namespace System.Drawing
                
                public void FillClosedCurve (Brush brush, PointF [] points)
                {
-                      Status status = GDIPlus.GdipFillClosedCurve (nativeObject, brush.NativeObject, points, points.Length);
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
+                       Status status = GDIPlus.GdipFillClosedCurve (nativeObject, brush.NativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                
                public void FillClosedCurve (Brush brush, Point [] points)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillClosedCurveI (nativeObject, brush.NativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
@@ -1076,126 +1440,196 @@ namespace System.Drawing
                
                public void FillClosedCurve (Brush brush, PointF [] points, FillMode fillmode)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        FillClosedCurve (brush, points, fillmode, 0.5f);
                }
                
                public void FillClosedCurve (Brush brush, Point [] points, FillMode fillmode)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        FillClosedCurve (brush, points, fillmode, 0.5f);
                }
 
                public void FillClosedCurve (Brush brush, PointF [] points, FillMode fillmode, float tension)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillClosedCurve2 (nativeObject, brush.NativeObject, points, points.Length, tension, fillmode);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillClosedCurve (Brush brush, Point [] points, FillMode fillmode, float tension)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillClosedCurve2I (nativeObject, brush.NativeObject, points, points.Length, tension, fillmode);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillEllipse (Brush brush, Rectangle rect)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        FillEllipse (brush, rect.X, rect.Y, rect.Width, rect.Height);
                }
 
                public void FillEllipse (Brush brush, RectangleF rect)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        FillEllipse (brush, rect.X, rect.Y, rect.Width, rect.Height);
                }
 
                public void FillEllipse (Brush brush, float x, float y, float width, float height)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                         Status status = GDIPlus.GdipFillEllipse (nativeObject, brush.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillEllipse (Brush brush, int x, int y, int width, int height)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        Status status = GDIPlus.GdipFillEllipseI (nativeObject, brush.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPath (Brush brush, GraphicsPath path)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
                        Status status = GDIPlus.GdipFillPath (nativeObject, brush.NativeObject,  path.NativeObject);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPie (Brush brush, Rectangle rect, float startAngle, float sweepAngle)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        Status status = GDIPlus.GdipFillPie (nativeObject, brush.NativeObject, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPie (Brush brush, int x, int y, int width, int height, int startAngle, int sweepAngle)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        Status status = GDIPlus.GdipFillPieI (nativeObject, brush.NativeObject, x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPie (Brush brush, float x, float y, float width, float height, float startAngle, float sweepAngle)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                        Status status = GDIPlus.GdipFillPie (nativeObject, brush.NativeObject, x, y, width, height, startAngle, sweepAngle);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPolygon (Brush brush, PointF [] points)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillPolygon2 (nativeObject, brush.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPolygon (Brush brush, Point [] points)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillPolygon2I (nativeObject, brush.nativeObject, points, points.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPolygon (Brush brush, Point [] points, FillMode fillMode)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillPolygonI (nativeObject, brush.nativeObject, points, points.Length, fillMode);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillPolygon (Brush brush, PointF [] points, FillMode fillMode)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (points == null)
+                               throw new ArgumentNullException ("points");
                        Status status = GDIPlus.GdipFillPolygon (nativeObject, brush.nativeObject, points, points.Length, fillMode);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillRectangle (Brush brush, RectangleF rect)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                         FillRectangle (brush, rect.Left, rect.Top, rect.Width, rect.Height);
                }
 
                public void FillRectangle (Brush brush, Rectangle rect)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
                         FillRectangle (brush, rect.Left, rect.Top, rect.Width, rect.Height);
                }
 
                public void FillRectangle (Brush brush, int x, int y, int width, int height)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       
                        Status status = GDIPlus.GdipFillRectangleI (nativeObject, brush.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillRectangle (Brush brush, float x, float y, float width, float height)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       
                        Status status = GDIPlus.GdipFillRectangle (nativeObject, brush.nativeObject, x, y, width, height);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillRectangles (Brush brush, Rectangle [] rects)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (rects == null)
+                               throw new ArgumentNullException ("rects");
+
                        Status status = GDIPlus.GdipFillRectanglesI (nativeObject, brush.nativeObject, rects, rects.Length);
                        GDIPlus.CheckStatus (status);
                }
 
                public void FillRectangles (Brush brush, RectangleF [] rects)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (rects == null)
+                               throw new ArgumentNullException ("rects");
+
                        Status status = GDIPlus.GdipFillRectangles (nativeObject, brush.nativeObject, rects, rects.Length);
                        GDIPlus.CheckStatus (status);
                }
@@ -1203,6 +1637,11 @@ namespace System.Drawing
                
                public void FillRegion (Brush brush, Region region)
                {
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (region == null)
+                               throw new ArgumentNullException ("region");
+                       
                        Status status = GDIPlus.GdipFillRegion (nativeObject, brush.NativeObject, region.NativeObject);                  
                         GDIPlus.CheckStatus(status);
                }
@@ -1216,6 +1655,10 @@ namespace System.Drawing
                
                public void Flush (FlushIntention intention)
                {
+                       if (nativeObject == IntPtr.Zero) {
+                               return;
+                       }
+
                        Status status = GDIPlus.GdipFlush (nativeObject, intention);
                         GDIPlus.CheckStatus (status);                    
                        if (GDIPlus.UseQuartzDrawable || GDIPlus.UseCocoaDrawable)
@@ -1291,12 +1734,8 @@ namespace System.Drawing
                {
                        IntPtr graphics;
 
-                       if (image == null) throw new ArgumentException ();
-
-                       if ((image.PixelFormat & PixelFormat.Indexed) != PixelFormat.Undefined) {\r
-                               // And MS ignores its own rules again\r
-                               throw new Exception ("A Graphics object cannot be created from an image that has an indexed pixel format");\r
-                       }\r
+                       if (image == null) 
+                               throw new ArgumentNullException ("image");
 
                        Status status = GDIPlus.GdipGetImageGraphicsContext (image.nativeObject, out graphics);
                        GDIPlus.CheckStatus (status);
@@ -1328,11 +1767,16 @@ namespace System.Drawing
                        throw new NotImplementedException ();
                }
 
+#if !NET_2_0
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+#endif
                public IntPtr GetHdc ()
                {
                        IntPtr hdc;
                        GDIPlus.CheckStatus (GDIPlus.GdipGetDC (this.nativeObject, out hdc));
+#if NET_2_0
+                       deviceContextHdc = hdc;
+#endif
                        return hdc;
                }
 
@@ -1350,6 +1794,8 @@ namespace System.Drawing
                
                public void IntersectClip (Region region)
                {
+                       if (region == null)
+                               throw new ArgumentNullException ("region");
                        Status status = GDIPlus.GdipSetClipRegion (nativeObject, region.NativeObject, CombineMode.Intersect);
                        GDIPlus.CheckStatus (status);
                }
@@ -1594,10 +2040,28 @@ namespace System.Drawing
                {
                        Status status = GDIPlus.GdipReleaseDC (nativeObject, hdc);
                        GDIPlus.CheckStatus (status);
+#if NET_2_0
+                       if (hdc == deviceContextHdc)
+                               deviceContextHdc = IntPtr.Zero;
+#endif
                }
 
+#if NET_2_0
+               public void ReleaseHdc ()\r
+               {\r
+                       if (deviceContextHdc == IntPtr.Zero)
+                               throw new ArgumentException ("Invalid Handle");
+
+                       ReleaseHdc (deviceContextHdc);
+               }
+#endif
+
                [MonoTODO]
+#if NET_2_0
+               [EditorBrowsable (EditorBrowsableState.Never)]
+#else
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+#endif
                [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public void ReleaseHdcInternal (IntPtr hdc)
                {
@@ -1685,6 +2149,9 @@ namespace System.Drawing
                
                public void SetClip (Graphics g, CombineMode combineMode)
                {
+                       if (g == null)
+                               throw new ArgumentNullException ("g");
+                       
                        Status status = GDIPlus.GdipSetClipGraphics (nativeObject, g.NativeObject, combineMode);
                        GDIPlus.CheckStatus (status);
                }
@@ -1706,6 +2173,8 @@ namespace System.Drawing
                
                public void SetClip (Region region, CombineMode combineMode)
                {
+                       if (region == null)
+                               throw new ArgumentNullException ("region");
                        Status status =   GDIPlus.GdipSetClipRegion(nativeObject,  region.NativeObject, combineMode); 
                        GDIPlus.CheckStatus (status);
                }
@@ -1713,6 +2182,8 @@ namespace System.Drawing
                
                public void SetClip (GraphicsPath path, CombineMode combineMode)
                {
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
                        Status status = GDIPlus.GdipSetClipPath (nativeObject, path.NativeObject, combineMode);
                        GDIPlus.CheckStatus (status);
                }
@@ -1720,6 +2191,9 @@ namespace System.Drawing
                
                public void TransformPoints (CoordinateSpace destSpace, CoordinateSpace srcSpace, PointF [] pts)
                {
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
+
                        IntPtr ptrPt =  GDIPlus.FromPointToUnManagedMemory (pts);
             
                         Status status = GDIPlus.GdipTransformPoints (nativeObject, destSpace, srcSpace,  ptrPt, pts.Length);
@@ -1731,6 +2205,8 @@ namespace System.Drawing
 
                public void TransformPoints (CoordinateSpace destSpace, CoordinateSpace srcSpace, Point [] pts)
                {                                               
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
                         IntPtr ptrPt =  GDIPlus.FromPointToUnManagedMemoryI (pts);
             
                         Status status = GDIPlus.GdipTransformPointsI (nativeObject, destSpace, srcSpace, ptrPt, pts.Length);
@@ -1896,6 +2372,7 @@ namespace System.Drawing
                        }
                }
 
+               [MonoTODO ("not supported by libgdiplus")]
                public PixelOffsetMode PixelOffsetMode {
                        get {
                                PixelOffsetMode pixelOffset = PixelOffsetMode.Invalid;
@@ -1939,6 +2416,7 @@ namespace System.Drawing
                        }
                }
 
+               [MonoTODO ("not supported by libgdiplus")]
                public int TextContrast {
                        get {   
                                 int contrast;
@@ -1977,6 +2455,9 @@ namespace System.Drawing
                                 return matrix;
                        }
                        set {
+                               if (value == null)
+                                       throw new ArgumentNullException ("value");
+                               
                                 Status status = GDIPlus.GdipSetWorldTransform (nativeObject, value.nativeMatrix);
                                GDIPlus.CheckStatus (status);
                        }
@@ -1991,6 +2472,14 @@ namespace System.Drawing
                                 return rect;
                        }
                }
+
+#if NET_2_0
+               [MonoTODO]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public object GetContextInfo ()
+               {
+                       throw new NotImplementedException ();
+               }
+#endif
        }
 }
-