Region.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / CommonUI / System / Drawing / Region.cs / 1 / Region.cs

                             	//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing { 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis; 
    using System;
    using Microsoft.Win32;
    using System.ComponentModel;
    using System.Drawing.Drawing2D; 
    using System.Drawing;
    using System.Drawing.Internal; 
    using System.Globalization; 

 
    /**
     * Represent a Region object
     */
    ///  
    /// 
    ///     
    ///       Describes the interior of a graphics shape 
    ///       composed of rectangles and paths.
    ///     
    /// 
    public sealed class Region : MarshalByRefObject, IDisposable {
#if FINALIZATION_WATCH
        private string allocationSite = Graphics.GetAllocationStack(); 
#endif
 
        /** 
         * Construct a new region object
         */ 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  class. 
        ///    
        ///  
        public Region() { 
            IntPtr region = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCreateRegion(out region);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            SetNativeRegion(region); 
        } 

        ///  
        /// 
        ///    
        ///       Initializes a new instance of the  class from the specified  .
        ///     
        /// 
        public Region(RectangleF rect) { 
            IntPtr region = IntPtr.Zero; 

            GPRECTF gprectf = rect.ToGPRECTF(); 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRect(ref gprectf, out region);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            SetNativeRegion(region); 
        }
 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  class from the specified . 
        ///    
        ///  
        public Region(Rectangle rect) { 
            IntPtr region = IntPtr.Zero;
 
            GPRECT gprect = new GPRECT(rect);

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRectI(ref gprect, out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            SetNativeRegion(region);
        } 

        /// 
        /// 
        ///     
        ///       Initializes a new instance of the  class
        ///       with the specified . 
        ///     
        /// 
        public Region(GraphicsPath path) { 
            if (path == null)
                throw new ArgumentNullException("path");

            IntPtr region = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionPath(new HandleRef(path, path.nativePath), out region); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            SetNativeRegion(region);
        }
 
        /// 
        ///  
        ///    Initializes a new instance of the  class 
        ///    from the specified data.
        ///  
        public Region(RegionData rgnData) {
            if (rgnData == null)
                throw new ArgumentNullException("rgnData");
            IntPtr region = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRgnData(rgnData.Data, 
                                                         rgnData.Data.Length, 
                                                         out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            SetNativeRegion(region); 
        }
 
        internal Region(IntPtr nativeRegion) { 
            SetNativeRegion(nativeRegion);
        } 

        /// 
        /// 
        ///    Initializes a new instance of the  class 
        ///    from the specified existing .
        ///  
        public static Region FromHrgn(IntPtr hrgn) { 
            IntSecurity.ObjectFromWin32Handle.Demand();
 
            IntPtr region = IntPtr.Zero;

            int status = SafeNativeMethods.Gdip.GdipCreateRegionHrgn(new HandleRef(null, hrgn), out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return new Region(region);
        } 

        private void SetNativeRegion(IntPtr nativeRegion) {
            if (nativeRegion == IntPtr.Zero)
                throw new ArgumentNullException("nativeRegion"); 

            this.nativeRegion = nativeRegion; 
        } 

        /** 
         * Make a copy of the region object
         */
        /// 
        ///  
        ///    Creates an exact copy if this .
        ///  
        public Region Clone() { 
            IntPtr region = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCloneRegion(new HandleRef(this, nativeRegion), out region);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return new Region(region); 
        } 

        /** 
         * Dispose of resources associated with the
         */
        /// 
        ///  
        ///    
        ///       Cleans up Windows resources for this 
        ///    . 
        ///    
        ///  
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        } 
        void Dispose(bool disposing) {
#if FINALIZATION_WATCH 
            if (!disposing && nativeRegion != IntPtr.Zero) 
                Debug.WriteLine("**********************\nDisposed through finalization:\n" + allocationSite);
#endif 
            if (nativeRegion != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif
                    SafeNativeMethods.Gdip.GdipDeleteRegion(new HandleRef(this, nativeRegion)); 
#if DEBUG 
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif 
                }
                catch( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw; 
                    }
 
                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() ); 
                }
                finally{ 
                    nativeRegion = IntPtr.Zero;
                }
            }
        } 

        ///  
        ///  
        ///    Cleans up Windows resources for this
        /// . 
        /// 
        ~Region() {
            Dispose(false);
        } 

        /* 
         * Region operations 
         */
 
        /// 
        /// 
        ///    Initializes this  to an
        ///    infinite interior. 
        /// 
        public void MakeInfinite() { 
            int status = SafeNativeMethods.Gdip.GdipSetInfinite(new HandleRef(this, nativeRegion)); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///    Initializes this  to an 
        ///    empty interior. 
        /// 
        public void MakeEmpty() { 
            int status = SafeNativeMethods.Gdip.GdipSetEmpty(new HandleRef(this, nativeRegion));

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        // float version 
        /// 
        ///  
        ///    Updates this  to the intersection of itself
        ///    with the specified .
        /// 
        public void Intersect(RectangleF rect) { 
            GPRECTF gprectf = rect.ToGPRECTF();
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Intersect); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        // int version
        ///  
        /// 
        ///     
        ///       Updates this  to the intersection of itself with the specified 
        ///    .
        ///     
        /// 
        public void Intersect(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect);
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Intersect); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    
        ///       Updates this  to the intersection of itself with the specified 
        ///    .
        ///     
        ///  
        public void Intersect(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Intersect);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        } 

        ///  
        /// 
        ///    
        ///       Updates this  to the intersection of itself with the specified
        ///    . 
        ///    
        ///  
        public void Intersect(Region region) { 
            if (region == null)
                throw new ArgumentNullException("region"); 

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Intersect);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        /// 
        ///  
        ///     Releases the handle to the region handle.
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
        public void ReleaseHrgn(IntPtr regionHandle) { 
            IntSecurity.ObjectFromWin32Handle.Demand();
            if (regionHandle == IntPtr.Zero) { 
                throw new ArgumentNullException("regionHandle"); 
            }
 
            SafeNativeMethods.IntDeleteObject(new HandleRef(this, regionHandle));
        }

        // float version 
        /// 
        ///  
        ///     
        ///       Updates this  to the union of itself and the
        ///       specified . 
        ///    
        /// 
        public void Union(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Union);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // int version
        /// 
        ///  
        ///    Updates this  to the union of itself and the
        ///    specified . 
        ///  
        public void Union(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Union);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Updates this  to the union of itself and the 
        ///    specified .
        /// 
        public void Union(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Union); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///     
        ///       Updates this  to the union of itself and the specified . 
        ///    
        ///  
        public void Union(Region region) {
            if (region == null)
                throw new ArgumentNullException("region");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Union);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // float version
        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified . 
        ///  
        public void Xor(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Xor);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        // int version 
        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified .
        /// 
        public void Xor(Rectangle rect) { 
            GPRECT gprect = new GPRECT(rect);
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Xor); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified . 
        ///  
        public void Xor(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Xor);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        } 

        ///  
        /// 
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified .
        ///  
        public void Xor(Region region) {
            if (region == null) 
                throw new ArgumentNullException("region"); 

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Xor); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // float version 
        ///  
        /// 
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        /// 
        public void Exclude(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Exclude);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // int version
        /// 
        ///  
        ///    Updates this  to the portion of its interior
        ///    that does not intersect with the specified . 
        ///  
        public void Exclude(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Exclude);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        /// 
        public void Exclude(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), 
                                                       CombineMode.Exclude);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /// 
        ///  
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        ///  
        public void Exclude(Region region) {
            if (region == null)
                throw new ArgumentNullException("region");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion),
                                                         CombineMode.Exclude); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        // float version
        ///  
        /// 
        ///    Updates this  to the portion of the 
        ///    specified  that does not intersect with this . 
        /// 
        public void Complement(RectangleF rect) { 
            GPRECTF gprectf = rect.ToGPRECTF();
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Complement);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        // int version
        ///  
        /// 
        ///    Updates this  to the portion of the
        ///    specified  that does not intersect with this .
        ///  
        public void Complement(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Complement); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///    Updates this  to the portion of the 
        ///    specified  that does not intersect with this 
        /// .
        ///  
        public void Complement(GraphicsPath path) {
            if (path == null)
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Complement);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        /// 
        /// 
        ///     
        ///       Updates this  to the portion of the
        ///       specified  that does not intersect with this . 
        ///     
        /// 
        public void Complement(Region region) { 
            if (region == null)
                throw new ArgumentNullException("region");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Complement); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /**
         * Transform operations
         */
        ///  
        /// 
        ///    Offsets the coordinates of this  by the 
        ///    specified amount. 
        /// 
        public void Translate(float dx, float dy) { 
            int status = SafeNativeMethods.Gdip.GdipTranslateRegion(new HandleRef(this, nativeRegion), dx, dy);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Offsets the coordinates of this  by the 
        ///    specified amount.
        /// 
        public void Translate(int dx, int dy) {
            int status = SafeNativeMethods.Gdip.GdipTranslateRegionI(new HandleRef(this, nativeRegion), dx, dy); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    Transforms this  by the
        ///    specified . 
        /// 
        public void Transform(Matrix matrix) { 
            if (matrix == null) 
                throw new ArgumentNullException("matrix");
 
            int status = SafeNativeMethods.Gdip.GdipTransformRegion(new HandleRef(this, nativeRegion),
                                                     new HandleRef(matrix, matrix.nativeMatrix));

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        /**
         * Get region attributes 
         */
        /// 
        /// 
        ///    Returns a  that represents a rectangular 
        ///    region that bounds this  on the drawing surface of a .
        ///  
        public RectangleF GetBounds(Graphics g) { 
            if (g == null)
                throw new ArgumentNullException("g"); 

            GPRECTF gprectf = new GPRECTF();

            int status = SafeNativeMethods.Gdip.GdipGetRegionBounds(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), ref gprectf); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return gprectf.ToRectangleF(); 
        }

        /// 
        ///  
        ///    Returns a Windows handle to this  in the
        ///    specified graphics context. 
        /// 
        ///    Remarks from MSDN:
        ///         It is the caller's responsibility to call the GDI function 
        ///         DeleteObject to free the GDI region when it is no longer needed.
        /// 
        public IntPtr GetHrgn(Graphics g) {
            if (g == null) 
                throw new ArgumentNullException("g");
 
            IntPtr hrgn = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipGetRegionHRgn(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out hrgn); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return hrgn;
        } 
 
        /// 
        ///  
        ///    
        ///       Tests whether this  has an
        ///       empty interior on the specified drawing surface.
        ///     
        /// 
        public bool IsEmpty(Graphics g) { 
            if (g == null) 
                throw new ArgumentNullException("g");
 
            int isEmpty;
            int status = SafeNativeMethods.Gdip.GdipIsEmptyRegion(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out isEmpty);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isEmpty != 0; 
        }
 
        /// 
        /// 
        ///    
        ///       Tests whether this  has 
        ///       an infinite interior on the specified drawing surface.
        ///     
        ///  
        public bool IsInfinite(Graphics g) {
            if (g == null) 
                throw new ArgumentNullException("g");

            int isInfinite;
            int status = SafeNativeMethods.Gdip.GdipIsInfiniteRegion(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out isInfinite); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return isInfinite != 0; 
        }

        /// 
        ///  
        ///    
        ///       Tests whether the specified  is 
        ///       identical to this  
        ///       on the specified drawing surface.
        ///     
        /// 
        public bool Equals(Region region, Graphics g) {
            if (g == null)
                throw new ArgumentNullException("g"); 

            if (region == null) 
                throw new ArgumentNullException("region"); 

            int isEqual; 
            int status = SafeNativeMethods.Gdip.GdipIsEqualRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), new HandleRef(g, g.NativeGraphics), out isEqual);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return isEqual != 0; 
        } 

        ///  
        /// 
        ///    Returns a  that represents the
        ///    information that describes this .
        ///  
        public RegionData GetRegionData() {
 
            int regionSize = 0; 

            int status = SafeNativeMethods.Gdip.GdipGetRegionDataSize(new HandleRef(this, nativeRegion), out regionSize); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            if (regionSize == 0)
                return null; 
 
            byte[] regionData = new byte[regionSize];
 
            status = SafeNativeMethods.Gdip.GdipGetRegionData(new HandleRef(this, nativeRegion), regionData, regionSize, out regionSize);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return new RegionData(regionData); 
        } 

        /* 
         * Hit testing operations
         */
        // float version
        ///  
        /// 
        ///     
        ///       Tests whether the specified point is 
        ///       contained within this  in the specified graphics context.
        ///     
        /// 
        public bool IsVisible(float x, float y) {
            return IsVisible(new PointF(x, y), null);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether the specified  is contained within this . 
        ///    
        /// 
        public bool IsVisible(PointF point) {
            return IsVisible(point, null); 
        }
 
        ///  
        /// 
        ///     
        ///       Tests whether the specified point is contained within this  in the
        ///       specified graphics context.
        ///    
        ///  
        public bool IsVisible(float x, float y, Graphics g) {
            return IsVisible(new PointF(x, y), g); 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether the specified  is
        ///       contained within this  in the specified graphics context. 
        ///    
        ///  
        public bool IsVisible(PointF point, Graphics g) { 
            int isVisible;
 
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionPoint(new HandleRef(this, nativeRegion), point.X, point.Y,
                                                          new HandleRef(g, (g==null) ? IntPtr.Zero : g.NativeGraphics),
                                                          out isVisible);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return isVisible != 0;
        } 

        /// 
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this 
        ///       . 
        ///     
        /// 
        public bool IsVisible(float x, float y, float width, float height) { 
            return IsVisible(new RectangleF(x, y, width, height), null);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this 
        ///    .
        ///     
        /// 
        public bool IsVisible(RectangleF rect) {
            return IsVisible(rect, null);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether the specified rectangle is contained within this  in the 
        ///       specified graphics context.
        ///    
        /// 
        public bool IsVisible(float x, float y, float width, float height, Graphics g) { 
            return IsVisible(new RectangleF(x, y, width, height), g);
        } 
 
        /// 
        ///  
        ///    
        ///       Tests whether the specified  is contained within this  in the specified graphics context.
        ///    
        ///  
        public bool IsVisible(RectangleF rect, Graphics g) {
 
            int isVisible = 0; 
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionRect(new HandleRef(this, nativeRegion), rect.X, rect.Y,
                                                         rect.Width, rect.Height, 
                                                         new HandleRef(g, (g==null) ? IntPtr.Zero : g.NativeGraphics),
                                                         out isVisible);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isVisible != 0; 
        }
 
        // int version
        /// 
        /// 
        ///     
        ///       Tests whether the specified point is contained within this  in the
        ///       specified graphics context. 
        ///     
        /// 
        public bool IsVisible(int x, int y, Graphics g) { 
            return IsVisible(new Point(x, y), g);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this . 
        ///    
        ///  
        public bool IsVisible(Point point) {
            return IsVisible(point, null);
        }
 
        /// 
        ///  
        ///     
        ///       Tests whether the specified  is contained within this
        ///     in the specified 
        ///       graphics context.
        ///    
        /// 
        public bool IsVisible(Point point, Graphics g) { 
            int isVisible = 0;
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionPointI(new HandleRef(this, nativeRegion), point.X, point.Y, 
                                                           new HandleRef(g, (g == null) ? IntPtr.Zero : g.NativeGraphics), 
                                                           out isVisible);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            return isVisible != 0; 
        }
 
        ///  
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this 
        ///       .
        ///    
        ///  
        public bool IsVisible(int x, int y, int width, int height) {
            return IsVisible(new Rectangle(x, y, width, height), null); 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether the specified  is contained within this
        ///    . 
        ///    
        ///  
        public bool IsVisible(Rectangle rect) { 
            return IsVisible(rect, null);
        } 

        /// 
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this  in the
        ///       specified graphics context. 
        ///     
        /// 
        public bool IsVisible(int x, int y, int width, int height, Graphics g) { 
            return IsVisible(new Rectangle(x, y, width, height), g);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this 
        ///    
        ///    in the specified graphics context. 
        /// 
        /// 
        public bool IsVisible(Rectangle rect, Graphics g) {
 
            int isVisible = 0;
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionRectI(new HandleRef(this, nativeRegion), rect.X, rect.Y, 
                                                          rect.Width, rect.Height, 
                                                          new HandleRef(g, (g == null) ? IntPtr.Zero : g.NativeGraphics),
                                                          out isVisible); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isVisible != 0;
        } 
 
        /// 
        ///  
        ///    
        ///       Returns an array of 
        ///       objects that approximate this Region on the specified
        ///     
        /// 
        [SuppressMessage("Microsoft.Performance", "CA1808:AvoidCallsThatBoxValueTypes")] 
        public RectangleF[] GetRegionScans(Matrix matrix) { 
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int count = 0;

            // call first time to get actual count of rectangles 

            int status = SafeNativeMethods.Gdip.GdipGetRegionScansCount(new HandleRef(this, nativeRegion), 
                                                         out count, 
                                                         new HandleRef(matrix, matrix.nativeMatrix));
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            RectangleF[] rectangles; 
            int rectsize = (int)Marshal.SizeOf(typeof(GPRECTF));
            IntPtr memoryRects = Marshal.AllocHGlobal(rectsize*count); 
 
            try {
                status = SafeNativeMethods.Gdip.GdipGetRegionScans(new HandleRef(this, nativeRegion), 
                    memoryRects,
                    out count,
                    new HandleRef(matrix, matrix.nativeMatrix));
 
                if (status != SafeNativeMethods.Gdip.Ok) {
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                } 

                int index; 
                GPRECTF gprectf = new GPRECTF();

                rectangles = new RectangleF[count];
 
                for (index=0; index
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing { 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis; 
    using System;
    using Microsoft.Win32;
    using System.ComponentModel;
    using System.Drawing.Drawing2D; 
    using System.Drawing;
    using System.Drawing.Internal; 
    using System.Globalization; 

 
    /**
     * Represent a Region object
     */
    ///  
    /// 
    ///     
    ///       Describes the interior of a graphics shape 
    ///       composed of rectangles and paths.
    ///     
    /// 
    public sealed class Region : MarshalByRefObject, IDisposable {
#if FINALIZATION_WATCH
        private string allocationSite = Graphics.GetAllocationStack(); 
#endif
 
        /** 
         * Construct a new region object
         */ 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  class. 
        ///    
        ///  
        public Region() { 
            IntPtr region = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCreateRegion(out region);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            SetNativeRegion(region); 
        } 

        ///  
        /// 
        ///    
        ///       Initializes a new instance of the  class from the specified  .
        ///     
        /// 
        public Region(RectangleF rect) { 
            IntPtr region = IntPtr.Zero; 

            GPRECTF gprectf = rect.ToGPRECTF(); 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRect(ref gprectf, out region);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            SetNativeRegion(region); 
        }
 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  class from the specified . 
        ///    
        ///  
        public Region(Rectangle rect) { 
            IntPtr region = IntPtr.Zero;
 
            GPRECT gprect = new GPRECT(rect);

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRectI(ref gprect, out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            SetNativeRegion(region);
        } 

        /// 
        /// 
        ///     
        ///       Initializes a new instance of the  class
        ///       with the specified . 
        ///     
        /// 
        public Region(GraphicsPath path) { 
            if (path == null)
                throw new ArgumentNullException("path");

            IntPtr region = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionPath(new HandleRef(path, path.nativePath), out region); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            SetNativeRegion(region);
        }
 
        /// 
        ///  
        ///    Initializes a new instance of the  class 
        ///    from the specified data.
        ///  
        public Region(RegionData rgnData) {
            if (rgnData == null)
                throw new ArgumentNullException("rgnData");
            IntPtr region = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipCreateRegionRgnData(rgnData.Data, 
                                                         rgnData.Data.Length, 
                                                         out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            SetNativeRegion(region); 
        }
 
        internal Region(IntPtr nativeRegion) { 
            SetNativeRegion(nativeRegion);
        } 

        /// 
        /// 
        ///    Initializes a new instance of the  class 
        ///    from the specified existing .
        ///  
        public static Region FromHrgn(IntPtr hrgn) { 
            IntSecurity.ObjectFromWin32Handle.Demand();
 
            IntPtr region = IntPtr.Zero;

            int status = SafeNativeMethods.Gdip.GdipCreateRegionHrgn(new HandleRef(null, hrgn), out region);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return new Region(region);
        } 

        private void SetNativeRegion(IntPtr nativeRegion) {
            if (nativeRegion == IntPtr.Zero)
                throw new ArgumentNullException("nativeRegion"); 

            this.nativeRegion = nativeRegion; 
        } 

        /** 
         * Make a copy of the region object
         */
        /// 
        ///  
        ///    Creates an exact copy if this .
        ///  
        public Region Clone() { 
            IntPtr region = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCloneRegion(new HandleRef(this, nativeRegion), out region);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return new Region(region); 
        } 

        /** 
         * Dispose of resources associated with the
         */
        /// 
        ///  
        ///    
        ///       Cleans up Windows resources for this 
        ///    . 
        ///    
        ///  
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        } 
        void Dispose(bool disposing) {
#if FINALIZATION_WATCH 
            if (!disposing && nativeRegion != IntPtr.Zero) 
                Debug.WriteLine("**********************\nDisposed through finalization:\n" + allocationSite);
#endif 
            if (nativeRegion != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif
                    SafeNativeMethods.Gdip.GdipDeleteRegion(new HandleRef(this, nativeRegion)); 
#if DEBUG 
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif 
                }
                catch( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw; 
                    }
 
                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() ); 
                }
                finally{ 
                    nativeRegion = IntPtr.Zero;
                }
            }
        } 

        ///  
        ///  
        ///    Cleans up Windows resources for this
        /// . 
        /// 
        ~Region() {
            Dispose(false);
        } 

        /* 
         * Region operations 
         */
 
        /// 
        /// 
        ///    Initializes this  to an
        ///    infinite interior. 
        /// 
        public void MakeInfinite() { 
            int status = SafeNativeMethods.Gdip.GdipSetInfinite(new HandleRef(this, nativeRegion)); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///    Initializes this  to an 
        ///    empty interior. 
        /// 
        public void MakeEmpty() { 
            int status = SafeNativeMethods.Gdip.GdipSetEmpty(new HandleRef(this, nativeRegion));

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        // float version 
        /// 
        ///  
        ///    Updates this  to the intersection of itself
        ///    with the specified .
        /// 
        public void Intersect(RectangleF rect) { 
            GPRECTF gprectf = rect.ToGPRECTF();
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Intersect); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        // int version
        ///  
        /// 
        ///     
        ///       Updates this  to the intersection of itself with the specified 
        ///    .
        ///     
        /// 
        public void Intersect(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect);
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Intersect); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    
        ///       Updates this  to the intersection of itself with the specified 
        ///    .
        ///     
        ///  
        public void Intersect(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Intersect);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        } 

        ///  
        /// 
        ///    
        ///       Updates this  to the intersection of itself with the specified
        ///    . 
        ///    
        ///  
        public void Intersect(Region region) { 
            if (region == null)
                throw new ArgumentNullException("region"); 

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Intersect);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        /// 
        ///  
        ///     Releases the handle to the region handle.
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
        public void ReleaseHrgn(IntPtr regionHandle) { 
            IntSecurity.ObjectFromWin32Handle.Demand();
            if (regionHandle == IntPtr.Zero) { 
                throw new ArgumentNullException("regionHandle"); 
            }
 
            SafeNativeMethods.IntDeleteObject(new HandleRef(this, regionHandle));
        }

        // float version 
        /// 
        ///  
        ///     
        ///       Updates this  to the union of itself and the
        ///       specified . 
        ///    
        /// 
        public void Union(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Union);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // int version
        /// 
        ///  
        ///    Updates this  to the union of itself and the
        ///    specified . 
        ///  
        public void Union(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Union);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Updates this  to the union of itself and the 
        ///    specified .
        /// 
        public void Union(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Union); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///     
        ///       Updates this  to the union of itself and the specified . 
        ///    
        ///  
        public void Union(Region region) {
            if (region == null)
                throw new ArgumentNullException("region");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Union);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // float version
        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified . 
        ///  
        public void Xor(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Xor);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        // int version 
        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified .
        /// 
        public void Xor(Rectangle rect) { 
            GPRECT gprect = new GPRECT(rect);
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Xor); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        /// 
        ///  
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified . 
        ///  
        public void Xor(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Xor);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        } 

        ///  
        /// 
        ///    Updates this  to the union minus the
        ///    intersection of itself with the specified .
        ///  
        public void Xor(Region region) {
            if (region == null) 
                throw new ArgumentNullException("region"); 

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Xor); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // float version 
        ///  
        /// 
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        /// 
        public void Exclude(RectangleF rect) {
            GPRECTF gprectf = new GPRECTF(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Exclude);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        // int version
        /// 
        ///  
        ///    Updates this  to the portion of its interior
        ///    that does not intersect with the specified . 
        ///  
        public void Exclude(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Exclude);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        /// 
        public void Exclude(GraphicsPath path) {
            if (path == null) 
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), 
                                                       CombineMode.Exclude);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /// 
        ///  
        ///    Updates this  to the portion of its interior 
        ///    that does not intersect with the specified .
        ///  
        public void Exclude(Region region) {
            if (region == null)
                throw new ArgumentNullException("region");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion),
                                                         CombineMode.Exclude); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        // float version
        ///  
        /// 
        ///    Updates this  to the portion of the 
        ///    specified  that does not intersect with this . 
        /// 
        public void Complement(RectangleF rect) { 
            GPRECTF gprectf = rect.ToGPRECTF();
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRect(new HandleRef(this, nativeRegion), ref gprectf, CombineMode.Complement);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        // int version
        ///  
        /// 
        ///    Updates this  to the portion of the
        ///    specified  that does not intersect with this .
        ///  
        public void Complement(Rectangle rect) {
            GPRECT gprect = new GPRECT(rect); 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionRectI(new HandleRef(this, nativeRegion), ref gprect, CombineMode.Complement); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

        ///  
        /// 
        ///    Updates this  to the portion of the 
        ///    specified  that does not intersect with this 
        /// .
        ///  
        public void Complement(GraphicsPath path) {
            if (path == null)
                throw new ArgumentNullException("path");
 
            int status = SafeNativeMethods.Gdip.GdipCombineRegionPath(new HandleRef(this, nativeRegion), new HandleRef(path, path.nativePath), CombineMode.Complement);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        /// 
        /// 
        ///     
        ///       Updates this  to the portion of the
        ///       specified  that does not intersect with this . 
        ///     
        /// 
        public void Complement(Region region) { 
            if (region == null)
                throw new ArgumentNullException("region");

            int status = SafeNativeMethods.Gdip.GdipCombineRegionRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), CombineMode.Complement); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /**
         * Transform operations
         */
        ///  
        /// 
        ///    Offsets the coordinates of this  by the 
        ///    specified amount. 
        /// 
        public void Translate(float dx, float dy) { 
            int status = SafeNativeMethods.Gdip.GdipTranslateRegion(new HandleRef(this, nativeRegion), dx, dy);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///    Offsets the coordinates of this  by the 
        ///    specified amount.
        /// 
        public void Translate(int dx, int dy) {
            int status = SafeNativeMethods.Gdip.GdipTranslateRegionI(new HandleRef(this, nativeRegion), dx, dy); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    Transforms this  by the
        ///    specified . 
        /// 
        public void Transform(Matrix matrix) { 
            if (matrix == null) 
                throw new ArgumentNullException("matrix");
 
            int status = SafeNativeMethods.Gdip.GdipTransformRegion(new HandleRef(this, nativeRegion),
                                                     new HandleRef(matrix, matrix.nativeMatrix));

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 
 
        /**
         * Get region attributes 
         */
        /// 
        /// 
        ///    Returns a  that represents a rectangular 
        ///    region that bounds this  on the drawing surface of a .
        ///  
        public RectangleF GetBounds(Graphics g) { 
            if (g == null)
                throw new ArgumentNullException("g"); 

            GPRECTF gprectf = new GPRECTF();

            int status = SafeNativeMethods.Gdip.GdipGetRegionBounds(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), ref gprectf); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return gprectf.ToRectangleF(); 
        }

        /// 
        ///  
        ///    Returns a Windows handle to this  in the
        ///    specified graphics context. 
        /// 
        ///    Remarks from MSDN:
        ///         It is the caller's responsibility to call the GDI function 
        ///         DeleteObject to free the GDI region when it is no longer needed.
        /// 
        public IntPtr GetHrgn(Graphics g) {
            if (g == null) 
                throw new ArgumentNullException("g");
 
            IntPtr hrgn = IntPtr.Zero; 

            int status = SafeNativeMethods.Gdip.GdipGetRegionHRgn(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out hrgn); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return hrgn;
        } 
 
        /// 
        ///  
        ///    
        ///       Tests whether this  has an
        ///       empty interior on the specified drawing surface.
        ///     
        /// 
        public bool IsEmpty(Graphics g) { 
            if (g == null) 
                throw new ArgumentNullException("g");
 
            int isEmpty;
            int status = SafeNativeMethods.Gdip.GdipIsEmptyRegion(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out isEmpty);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isEmpty != 0; 
        }
 
        /// 
        /// 
        ///    
        ///       Tests whether this  has 
        ///       an infinite interior on the specified drawing surface.
        ///     
        ///  
        public bool IsInfinite(Graphics g) {
            if (g == null) 
                throw new ArgumentNullException("g");

            int isInfinite;
            int status = SafeNativeMethods.Gdip.GdipIsInfiniteRegion(new HandleRef(this, nativeRegion), new HandleRef(g, g.NativeGraphics), out isInfinite); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return isInfinite != 0; 
        }

        /// 
        ///  
        ///    
        ///       Tests whether the specified  is 
        ///       identical to this  
        ///       on the specified drawing surface.
        ///     
        /// 
        public bool Equals(Region region, Graphics g) {
            if (g == null)
                throw new ArgumentNullException("g"); 

            if (region == null) 
                throw new ArgumentNullException("region"); 

            int isEqual; 
            int status = SafeNativeMethods.Gdip.GdipIsEqualRegion(new HandleRef(this, nativeRegion), new HandleRef(region, region.nativeRegion), new HandleRef(g, g.NativeGraphics), out isEqual);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return isEqual != 0; 
        } 

        ///  
        /// 
        ///    Returns a  that represents the
        ///    information that describes this .
        ///  
        public RegionData GetRegionData() {
 
            int regionSize = 0; 

            int status = SafeNativeMethods.Gdip.GdipGetRegionDataSize(new HandleRef(this, nativeRegion), out regionSize); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            if (regionSize == 0)
                return null; 
 
            byte[] regionData = new byte[regionSize];
 
            status = SafeNativeMethods.Gdip.GdipGetRegionData(new HandleRef(this, nativeRegion), regionData, regionSize, out regionSize);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return new RegionData(regionData); 
        } 

        /* 
         * Hit testing operations
         */
        // float version
        ///  
        /// 
        ///     
        ///       Tests whether the specified point is 
        ///       contained within this  in the specified graphics context.
        ///     
        /// 
        public bool IsVisible(float x, float y) {
            return IsVisible(new PointF(x, y), null);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether the specified  is contained within this . 
        ///    
        /// 
        public bool IsVisible(PointF point) {
            return IsVisible(point, null); 
        }
 
        ///  
        /// 
        ///     
        ///       Tests whether the specified point is contained within this  in the
        ///       specified graphics context.
        ///    
        ///  
        public bool IsVisible(float x, float y, Graphics g) {
            return IsVisible(new PointF(x, y), g); 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether the specified  is
        ///       contained within this  in the specified graphics context. 
        ///    
        ///  
        public bool IsVisible(PointF point, Graphics g) { 
            int isVisible;
 
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionPoint(new HandleRef(this, nativeRegion), point.X, point.Y,
                                                          new HandleRef(g, (g==null) ? IntPtr.Zero : g.NativeGraphics),
                                                          out isVisible);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return isVisible != 0;
        } 

        /// 
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this 
        ///       . 
        ///     
        /// 
        public bool IsVisible(float x, float y, float width, float height) { 
            return IsVisible(new RectangleF(x, y, width, height), null);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this 
        ///    .
        ///     
        /// 
        public bool IsVisible(RectangleF rect) {
            return IsVisible(rect, null);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether the specified rectangle is contained within this  in the 
        ///       specified graphics context.
        ///    
        /// 
        public bool IsVisible(float x, float y, float width, float height, Graphics g) { 
            return IsVisible(new RectangleF(x, y, width, height), g);
        } 
 
        /// 
        ///  
        ///    
        ///       Tests whether the specified  is contained within this  in the specified graphics context.
        ///    
        ///  
        public bool IsVisible(RectangleF rect, Graphics g) {
 
            int isVisible = 0; 
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionRect(new HandleRef(this, nativeRegion), rect.X, rect.Y,
                                                         rect.Width, rect.Height, 
                                                         new HandleRef(g, (g==null) ? IntPtr.Zero : g.NativeGraphics),
                                                         out isVisible);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isVisible != 0; 
        }
 
        // int version
        /// 
        /// 
        ///     
        ///       Tests whether the specified point is contained within this  in the
        ///       specified graphics context. 
        ///     
        /// 
        public bool IsVisible(int x, int y, Graphics g) { 
            return IsVisible(new Point(x, y), g);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this . 
        ///    
        ///  
        public bool IsVisible(Point point) {
            return IsVisible(point, null);
        }
 
        /// 
        ///  
        ///     
        ///       Tests whether the specified  is contained within this
        ///     in the specified 
        ///       graphics context.
        ///    
        /// 
        public bool IsVisible(Point point, Graphics g) { 
            int isVisible = 0;
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionPointI(new HandleRef(this, nativeRegion), point.X, point.Y, 
                                                           new HandleRef(g, (g == null) ? IntPtr.Zero : g.NativeGraphics), 
                                                           out isVisible);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            return isVisible != 0; 
        }
 
        ///  
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this 
        ///       .
        ///    
        ///  
        public bool IsVisible(int x, int y, int width, int height) {
            return IsVisible(new Rectangle(x, y, width, height), null); 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether the specified  is contained within this
        ///    . 
        ///    
        ///  
        public bool IsVisible(Rectangle rect) { 
            return IsVisible(rect, null);
        } 

        /// 
        /// 
        ///     
        ///       Tests whether the specified rectangle is contained within this  in the
        ///       specified graphics context. 
        ///     
        /// 
        public bool IsVisible(int x, int y, int width, int height, Graphics g) { 
            return IsVisible(new Rectangle(x, y, width, height), g);
        }

        ///  
        /// 
        ///     
        ///       Tests whether the specified  is contained within this 
        ///    
        ///    in the specified graphics context. 
        /// 
        /// 
        public bool IsVisible(Rectangle rect, Graphics g) {
 
            int isVisible = 0;
            int status = SafeNativeMethods.Gdip.GdipIsVisibleRegionRectI(new HandleRef(this, nativeRegion), rect.X, rect.Y, 
                                                          rect.Width, rect.Height, 
                                                          new HandleRef(g, (g == null) ? IntPtr.Zero : g.NativeGraphics),
                                                          out isVisible); 

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            return isVisible != 0;
        } 
 
        /// 
        ///  
        ///    
        ///       Returns an array of 
        ///       objects that approximate this Region on the specified
        ///     
        /// 
        [SuppressMessage("Microsoft.Performance", "CA1808:AvoidCallsThatBoxValueTypes")] 
        public RectangleF[] GetRegionScans(Matrix matrix) { 
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int count = 0;

            // call first time to get actual count of rectangles 

            int status = SafeNativeMethods.Gdip.GdipGetRegionScansCount(new HandleRef(this, nativeRegion), 
                                                         out count, 
                                                         new HandleRef(matrix, matrix.nativeMatrix));
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            RectangleF[] rectangles; 
            int rectsize = (int)Marshal.SizeOf(typeof(GPRECTF));
            IntPtr memoryRects = Marshal.AllocHGlobal(rectsize*count); 
 
            try {
                status = SafeNativeMethods.Gdip.GdipGetRegionScans(new HandleRef(this, nativeRegion), 
                    memoryRects,
                    out count,
                    new HandleRef(matrix, matrix.nativeMatrix));
 
                if (status != SafeNativeMethods.Gdip.Ok) {
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                } 

                int index; 
                GPRECTF gprectf = new GPRECTF();

                rectangles = new RectangleF[count];
 
                for (index=0; index

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK