PathGradientBrush.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CommonUI / System / Drawing / Advanced / PathGradientBrush.cs / 1305376 / PathGradientBrush.cs

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

namespace System.Drawing.Drawing2D { 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;
    using System.Drawing; 
    using System.ComponentModel;
    using Microsoft.Win32;
    using System.Drawing.Internal;
    using System; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.Versioning; 
 
    /**
     * Represent a PathGradient brush object 
     */
    /// 
    /// 
    ///    Encapsulates a  that fills the interior of a 
    ///  with a gradient.
    ///  
    public sealed class PathGradientBrush : Brush { 

        /** 
         * Create a new rectangle gradient brush object
         */
        /// 
        ///  
        ///    
        ///       Initializes a new instance of the  class with the specified points. 
        ///     
        /// 
        [ResourceExposure(ResourceScope.Process)] 
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(PointF[] points)
            : this(points, System.Drawing.Drawing2D.WrapMode.Clamp) {
        } 

        ///  
        ///  
        ///    Initializes a new instance of the  class with the specified points and
        ///    wrap mode. 
        /// 
        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public PathGradientBrush(PointF[] points, WrapMode wrapMode)
        { 
            if (points == null) 
                throw new ArgumentNullException("points");
 
            //validate the WrapMode enum
            //valid values are 0x0 to 0x4
            if (!ClientUtils.IsEnumValid(wrapMode, (int)wrapMode, (int)WrapMode.Tile, (int)WrapMode.Clamp))
            { 
                throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
            } 
 
            IntPtr brush = IntPtr.Zero;
            IntPtr pointsBuf = SafeNativeMethods.Gdip.ConvertPointToMemory(points); 
            try
            {
                int status = SafeNativeMethods.Gdip.GdipCreatePathGradient(new HandleRef(null, pointsBuf),
                                                            points.Length, 
                                                            (int)wrapMode,
                                                            out brush); 
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status); 

                SetNativeBrushInternal(brush);
            }
            finally 
            {
                // Inside SafeNativeMethods.Gdip.ConvertPointToMemory, Marshal.AllocHGlobal 
                // is used to allocate unmanaged memory. Therefore, we need to free it 
                // manually with Marshal.FreeHGlobal
                if (pointsBuf != IntPtr.Zero) 
                {
                    Marshal.FreeHGlobal(pointsBuf);
                }
            } 
        }
 
        ///  
        /// 
        ///     
        ///       Initializes a new instance of the  class with the
        ///       specified points.
        ///    
        ///  
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public PathGradientBrush(Point[] points) 
            : this(points, System.Drawing.Drawing2D.WrapMode.Clamp) {
        } 

        /// 
        /// 
        ///     
        ///       Initializes a new instance of the  class with the
        ///       specified points and wrap mode. 
        ///     
        /// 
        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")] 
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(Point[] points, WrapMode wrapMode)
        { 
            if (points == null)
                throw new ArgumentNullException("points"); 
 
            //validate the WrapMode enum
            if (!ClientUtils.IsEnumValid(wrapMode, (int)wrapMode, (int)WrapMode.Tile, (int)WrapMode.Clamp)) 
            {
                throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
            }
 
            IntPtr brush = IntPtr.Zero;
            IntPtr pointsBuf = SafeNativeMethods.Gdip.ConvertPointToMemory(points); 
            try 
            {
                int status = SafeNativeMethods.Gdip.GdipCreatePathGradientI(new HandleRef(null, pointsBuf), 
                                                             points.Length,
                                                             (int)wrapMode,
                                                             out brush);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status); 
 
                SetNativeBrushInternal(brush);
            } 
            finally
            {
                // Inside SafeNativeMethods.Gdip.ConvertPointToMemory, Marshal.AllocHGlobal
                // is used to allocate unmanaged memory. Therefore, we need to free it 
                // manually with Marshal.FreeHGlobal
                if (pointsBuf != IntPtr.Zero) 
                { 
                    Marshal.FreeHGlobal(pointsBuf);
                } 
            }
        }

        ///  
        /// 
        ///     
        ///       Initializes a new instance of the  
        ///       class with the specified path.
        ///     
        /// 
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(GraphicsPath path) 
        {
            if (path == null) 
                throw new ArgumentNullException("path"); 

            IntPtr brush = IntPtr.Zero; 
            int status = SafeNativeMethods.Gdip.GdipCreatePathGradientFromPath(new HandleRef(path, path.nativePath),
                                                                out brush);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            SetNativeBrushInternal(brush); 
        }
 
        /// 
        ///     Constructor to initialized this object to be owned by GDI+.
        /// 
        internal PathGradientBrush(IntPtr nativeBrush ) 
        {
            Debug.Assert( nativeBrush != IntPtr.Zero, "Initializing native brush with null." ); 
            SetNativeBrushInternal( nativeBrush ); 
        }
 
        /// 
        /// 
        ///    Creates an exact copy of this .
        ///  
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public override object Clone() { 
            IntPtr cloneBrush = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCloneBrush(new HandleRef(this, this.NativeBrush), out cloneBrush);

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

            return new PathGradientBrush(cloneBrush ); 
        } 

        /** 
         * Set/get center color attributes
         */
        /// 
        ///  
        ///    Gets or sets the color at the center of the
        ///    path gradient. 
        ///  
        public Color CenterColor
        { 
            get {
                int argb;

                int status = SafeNativeMethods.Gdip.GdipGetPathGradientCenterColor(new HandleRef(this, this.NativeBrush), out argb); 

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

                return Color.FromArgb(argb); 
            }

            set {
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientCenterColor(new HandleRef(this, this.NativeBrush), value.ToArgb()); 

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

        /**
         * Get/set colors
         * !! NOTE: We do not have methods for GetSurroundColor or SetSurroundColor, 
         *    May need to add usage of Collection class
         */ 
 
        private void _SetSurroundColors(Color[] colors)
        { 
            int count;

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientSurroundColorCount(new HandleRef(this, this.NativeBrush),
                                                                       out count); 

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

            if ((colors.Length > count) || (count <= 0)) 
                throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.InvalidParameter);

            count = colors.Length;
            int[] argbs = new int[count]; 
            for (int i=0; i 
        /// 
        ///     
        ///       Gets or sets an array of colors that
        ///       correspond to the points in the path this  fills.
        ///    
        ///  
        public Color[] SurroundColors
        { 
            get { return _GetSurroundColors();} 
            set { _SetSurroundColors(value);}
        } 

       /**
         * Set/get center point
         */ 
        /// 
        ///  
        ///     
        ///       Gets or sets the center point of the path
        ///       gradient. 
        ///    
        /// 
        public PointF CenterPoint
        { 
            get {
                GPPOINTF point = new GPPOINTF(); 
 
                int status = SafeNativeMethods.Gdip.GdipGetPathGradientCenterPoint(new HandleRef(this, this.NativeBrush), point);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);

                return point.ToPoint(); 
            }
 
            set { 
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientCenterPoint(new HandleRef(this, this.NativeBrush), new GPPOINTF(value));
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
            }
        } 

        /** 
         * Get source rectangle 
         */
        private RectangleF _GetRectangle() { 
            GPRECTF rect = new GPRECTF();

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientRect(new HandleRef(this, this.NativeBrush), ref rect);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return rect.ToRectangleF();
        } 

        /// 
        /// 
        ///     
        ///       Gets a bounding rectangle for this .
        ///     
        ///  
        public RectangleF Rectangle
        { 
            get { return _GetRectangle();}
        }

        /** 
         * Set/get blend factors
         */ 
        private Blend _GetBlend() { 
            Blend blend;
 
            // Figure out the size of blend factor array
            int retval = 0;
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientBlendCount(new HandleRef(this, this.NativeBrush), out retval);
 
            if (status != SafeNativeMethods.Gdip.Ok) {
                throw SafeNativeMethods.Gdip.StatusException(status); 
            } 

            // Allocate temporary native memory buffer 

            int count = retval;

            IntPtr factors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;
 
            try { 
                factors = Marshal.AllocHGlobal(4*count);
                positions = Marshal.AllocHGlobal(4*count); 

                // Retrieve horizontal blend factors

                status = SafeNativeMethods.Gdip.GdipGetPathGradientBlend(new HandleRef(this, this.NativeBrush), factors, positions, count); 

                if (status != SafeNativeMethods.Gdip.Ok) { 
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                }
 
                // Return the result in a managed array

                blend = new Blend(count);
 
                Marshal.Copy(factors, blend.Factors, 0, count);
                Marshal.Copy(positions, blend.Positions, 0, count); 
 
            }
            finally { 
                if( factors != IntPtr.Zero ) {
                    Marshal.FreeHGlobal(factors);
                }
                if( positions != IntPtr.Zero ) { 
                    Marshal.FreeHGlobal(positions);
                } 
            } 

            return blend; 
        }

        private void _SetBlend(Blend blend) {
            // Allocate temporary native memory buffer 
            // and copy input blend factors into it.
 
            int count = blend.Factors.Length; 

            IntPtr factors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;

            try {
                factors = Marshal.AllocHGlobal(4*count); 
                positions = Marshal.AllocHGlobal(4*count);
 
                Marshal.Copy(blend.Factors, 0, factors, count); 
                Marshal.Copy(blend.Positions, 0, positions, count);
 
                // Set blend factors

                int status = SafeNativeMethods.Gdip.GdipSetPathGradientBlend(new HandleRef(this, this.NativeBrush), new HandleRef(null, factors), new HandleRef(null, positions), count);
 
                if (status != SafeNativeMethods.Gdip.Ok) {
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                } 
            }
            finally { 
                if( factors != IntPtr.Zero ) {
                    Marshal.FreeHGlobal(factors);
                }
                if( positions != IntPtr.Zero ) { 
                    Marshal.FreeHGlobal(positions);
                } 
            } 
        }
 
        /// 
        /// 
        ///    
        ///       Gets or sets a  that specifies positions and factors 
        ///       that define a custom falloff for the gradient.
        ///     
        ///  
        public Blend Blend
        { 
            get { return _GetBlend();}
            set { _SetBlend(value);}
        }
 
        /*
         * SigmaBlend & LinearBlend 
         */ 

        ///  
        /// 
        ///    
        ///       Creates a gradient falloff based on a bell-shaped curve.
        ///     
        /// 
        public void SetSigmaBellShape(float focus) 
        { 
            SetSigmaBellShape(focus, (float)1.0);
        } 

        /// 
        /// 
        ///     
        ///       Creates a gradient falloff based on a bell-shaped curve.
        ///     
        ///  
        public void SetSigmaBellShape(float focus, float scale)
        { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientSigmaBlend(new HandleRef(this, this.NativeBrush), focus, scale);

            if (status != SafeNativeMethods.Gdip.Ok)
                 throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///     
        ///       Creates a triangular gradient.
        ///    
        /// 
        public void SetBlendTriangularShape(float focus) 
        {
            SetBlendTriangularShape(focus, (float)1.0); 
        } 

        ///  
        /// 
        ///    
        ///       Creates a triangular gradient.
        ///     
        /// 
        public void SetBlendTriangularShape(float focus, float scale) 
        { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientLinearBlend(new HandleRef(this, this.NativeBrush), focus, scale);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                 throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /*
         * Preset Color Blend 
         */ 

        private ColorBlend _GetInterpolationColors() { 
            ColorBlend blend;

            // Figure out the size of blend factor array
            int retval = 0; 
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientPresetBlendCount(new HandleRef(this, this.NativeBrush), out retval);
 
            if (status != SafeNativeMethods.Gdip.Ok) { 
                throw SafeNativeMethods.Gdip.StatusException(status);
            } 

            // If retVal is 0, then there is nothing to marshal.
            // In this case, we'll return an empty ColorBlend...
            // 
            if (retval == 0) {
                return new ColorBlend(); 
            } 

            // Allocate temporary native memory buffer 

            int count = retval;

            IntPtr colors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;
 
            try { 
                colors = Marshal.AllocHGlobal(4*count);
                positions = Marshal.AllocHGlobal(4*count); 

                // Retrieve horizontal blend factors

                status = SafeNativeMethods.Gdip.GdipGetPathGradientPresetBlend(new HandleRef(this, this.NativeBrush), colors, positions, count); 

                if (status != SafeNativeMethods.Gdip.Ok) { 
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                }
 
                // Return the result in a managed array

                blend = new ColorBlend(count);
 
                int[] argb = new int[count];
                Marshal.Copy(colors, argb, 0, count); 
                Marshal.Copy(positions, blend.Positions, 0, count); 

                // copy ARGB values into Color array of ColorBlend 
                blend.Colors = new Color[argb.Length];

                for (int i=0; i 
        /// 
        ///     
        ///       Gets or sets a  that defines a multi-color linear 
        ///       gradient.
        ///     
        /// 
        public ColorBlend InterpolationColors
        {
            get { return _GetInterpolationColors(); } 
            set { _SetInterpolationColors(value); }
        } 
 
        /**
         * Set/get brush transform 
         */
        private void _SetTransform(Matrix matrix) {
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int status = SafeNativeMethods.Gdip.GdipSetPathGradientTransform(new HandleRef(this, this.NativeBrush), new HandleRef(matrix, matrix.nativeMatrix)); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        private Matrix _GetTransform() {
            Matrix matrix = new Matrix(); 

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientTransform(new HandleRef(this, this.NativeBrush), new HandleRef(matrix, matrix.nativeMatrix)); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return matrix;
        }
 
        /// 
        ///  
        ///     
        ///       Gets or sets a  that defines a local geometrical
        ///       transform for this . 
        ///    
        /// 
        public Matrix Transform
        { 
            get { return _GetTransform();}
            set { _SetTransform(value);} 
        } 

        ///  
        /// 
        ///    
        ///       Resets the  property to
        ///       identity. 
        ///    
        ///  
        public void ResetTransform() { 
            int status = SafeNativeMethods.Gdip.GdipResetPathGradientTransform(new HandleRef(this, this.NativeBrush));
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /// 
        ///  
        ///     
        ///       Multiplies the  that represents the local geometrical
        ///       transform of this  by the specified  by prepending the specified . 
        ///    
        /// 
        public void MultiplyTransform(Matrix matrix)
        { 
            MultiplyTransform(matrix, MatrixOrder.Prepend);
        } 
 
        /// 
        ///  
        ///    
        ///       Multiplies the  that represents the local geometrical
        ///       transform of this  by the specified  in the specified order.
        ///     
        /// 
        public void MultiplyTransform(Matrix matrix, MatrixOrder order) 
        { 
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int status = SafeNativeMethods.Gdip.GdipMultiplyPathGradientTransform(new HandleRef(this, this.NativeBrush),
                                                new HandleRef(matrix, matrix.nativeMatrix),
                                                order); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    
        ///       Translates the local geometrical transform by the specified dimmensions. This 
        ///       method prepends the translation to the transform.
        ///     
        ///  
        public void TranslateTransform(float dx, float dy)
        { 
            TranslateTransform(dx, dy, MatrixOrder.Prepend);
        }

        ///  
        /// 
        ///     
        ///       Translates the local geometrical transform by the specified dimmensions in 
        ///       the specified order.
        ///     
        /// 
        public void TranslateTransform(float dx, float dy, MatrixOrder order)
        {
            int status = SafeNativeMethods.Gdip.GdipTranslatePathGradientTransform(new HandleRef(this, this.NativeBrush), 
                                                dx, dy, order);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        /// 
        /// 
        ///     
        ///       Scales the local geometric transform by the specified amounts. This method
        ///       prepends the scaling matrix to the transform. 
        ///     
        /// 
        public void ScaleTransform(float sx, float sy) 
        {
            ScaleTransform(sx, sy, MatrixOrder.Prepend);
        }
 
        /// 
        ///  
        ///     
        ///       Scales the local geometric transform by the specified amounts in the
        ///       specified order. 
        ///    
        /// 
        public void ScaleTransform(float sx, float sy, MatrixOrder order)
        { 
            int status = SafeNativeMethods.Gdip.GdipScalePathGradientTransform(new HandleRef(this, this.NativeBrush),
                                                sx, sy, order); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        /// 
        ///  
        ///    
        ///       Rotates the local geometric transform by the specified amount. This method 
        ///       prepends the rotation to the transform. 
        ///    
        ///  
        public void RotateTransform(float angle)
        {
            RotateTransform(angle, MatrixOrder.Prepend);
        } 

        ///  
        ///  
        ///    
        ///       Rotates the local geometric transform by the specified amount in the 
        ///       specified order.
        ///    
        /// 
        public void RotateTransform(float angle, MatrixOrder order) 
        {
            int status = SafeNativeMethods.Gdip.GdipRotatePathGradientTransform(new HandleRef(this, this.NativeBrush), 
                                                angle, order); 

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

        /** 
         * Set/get brush focus scales
         */ 
        ///  
        /// 
        ///    Gets or sets the focus point for the 
        ///    gradient falloff.
        /// 
        public PointF FocusScales
        { 
            get {
                float[] scaleX = new float[] { 0.0f }; 
                float[] scaleY = new float[] { 0.0f }; 

                int status = SafeNativeMethods.Gdip.GdipGetPathGradientFocusScales(new HandleRef(this, this.NativeBrush), scaleX, scaleY); 

                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
 
                return new PointF(scaleX[0], scaleY[0]);
            } 
            set { 
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientFocusScales(new HandleRef(this, this.NativeBrush), value.X, value.Y);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
            }
        } 

        /** 
         * Set/get brush wrapping mode 
         */
        private void _SetWrapMode(WrapMode wrapMode) { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientWrapMode(new HandleRef(this, this.NativeBrush), (int) wrapMode);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        private WrapMode _GetWrapMode() { 
            int mode = 0;
 
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientWrapMode(new HandleRef(this, this.NativeBrush), out mode);

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

            return (WrapMode) mode; 
        } 

        ///  
        /// 
        ///    
        ///       Gets or sets a  that indicates the wrap mode for this
        ///    . 
        ///    
        ///  
        public WrapMode WrapMode 
        {
            get { 
                return _GetWrapMode();
            }
            set {
                //validate the WrapMode enum 
                if (!ClientUtils.IsEnumValid(value, (int)value, (int)WrapMode.Tile, (int)WrapMode.Clamp))
                { 
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(WrapMode)); 
                }
 
                _SetWrapMode(value);
            }
        }
    } 

} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing.Drawing2D { 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;
    using System.Drawing; 
    using System.ComponentModel;
    using Microsoft.Win32;
    using System.Drawing.Internal;
    using System; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.Versioning; 
 
    /**
     * Represent a PathGradient brush object 
     */
    /// 
    /// 
    ///    Encapsulates a  that fills the interior of a 
    ///  with a gradient.
    ///  
    public sealed class PathGradientBrush : Brush { 

        /** 
         * Create a new rectangle gradient brush object
         */
        /// 
        ///  
        ///    
        ///       Initializes a new instance of the  class with the specified points. 
        ///     
        /// 
        [ResourceExposure(ResourceScope.Process)] 
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(PointF[] points)
            : this(points, System.Drawing.Drawing2D.WrapMode.Clamp) {
        } 

        ///  
        ///  
        ///    Initializes a new instance of the  class with the specified points and
        ///    wrap mode. 
        /// 
        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public PathGradientBrush(PointF[] points, WrapMode wrapMode)
        { 
            if (points == null) 
                throw new ArgumentNullException("points");
 
            //validate the WrapMode enum
            //valid values are 0x0 to 0x4
            if (!ClientUtils.IsEnumValid(wrapMode, (int)wrapMode, (int)WrapMode.Tile, (int)WrapMode.Clamp))
            { 
                throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
            } 
 
            IntPtr brush = IntPtr.Zero;
            IntPtr pointsBuf = SafeNativeMethods.Gdip.ConvertPointToMemory(points); 
            try
            {
                int status = SafeNativeMethods.Gdip.GdipCreatePathGradient(new HandleRef(null, pointsBuf),
                                                            points.Length, 
                                                            (int)wrapMode,
                                                            out brush); 
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status); 

                SetNativeBrushInternal(brush);
            }
            finally 
            {
                // Inside SafeNativeMethods.Gdip.ConvertPointToMemory, Marshal.AllocHGlobal 
                // is used to allocate unmanaged memory. Therefore, we need to free it 
                // manually with Marshal.FreeHGlobal
                if (pointsBuf != IntPtr.Zero) 
                {
                    Marshal.FreeHGlobal(pointsBuf);
                }
            } 
        }
 
        ///  
        /// 
        ///     
        ///       Initializes a new instance of the  class with the
        ///       specified points.
        ///    
        ///  
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public PathGradientBrush(Point[] points) 
            : this(points, System.Drawing.Drawing2D.WrapMode.Clamp) {
        } 

        /// 
        /// 
        ///     
        ///       Initializes a new instance of the  class with the
        ///       specified points and wrap mode. 
        ///     
        /// 
        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")] 
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(Point[] points, WrapMode wrapMode)
        { 
            if (points == null)
                throw new ArgumentNullException("points"); 
 
            //validate the WrapMode enum
            if (!ClientUtils.IsEnumValid(wrapMode, (int)wrapMode, (int)WrapMode.Tile, (int)WrapMode.Clamp)) 
            {
                throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
            }
 
            IntPtr brush = IntPtr.Zero;
            IntPtr pointsBuf = SafeNativeMethods.Gdip.ConvertPointToMemory(points); 
            try 
            {
                int status = SafeNativeMethods.Gdip.GdipCreatePathGradientI(new HandleRef(null, pointsBuf), 
                                                             points.Length,
                                                             (int)wrapMode,
                                                             out brush);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status); 
 
                SetNativeBrushInternal(brush);
            } 
            finally
            {
                // Inside SafeNativeMethods.Gdip.ConvertPointToMemory, Marshal.AllocHGlobal
                // is used to allocate unmanaged memory. Therefore, we need to free it 
                // manually with Marshal.FreeHGlobal
                if (pointsBuf != IntPtr.Zero) 
                { 
                    Marshal.FreeHGlobal(pointsBuf);
                } 
            }
        }

        ///  
        /// 
        ///     
        ///       Initializes a new instance of the  
        ///       class with the specified path.
        ///     
        /// 
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)]
        public PathGradientBrush(GraphicsPath path) 
        {
            if (path == null) 
                throw new ArgumentNullException("path"); 

            IntPtr brush = IntPtr.Zero; 
            int status = SafeNativeMethods.Gdip.GdipCreatePathGradientFromPath(new HandleRef(path, path.nativePath),
                                                                out brush);

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
 
            SetNativeBrushInternal(brush); 
        }
 
        /// 
        ///     Constructor to initialized this object to be owned by GDI+.
        /// 
        internal PathGradientBrush(IntPtr nativeBrush ) 
        {
            Debug.Assert( nativeBrush != IntPtr.Zero, "Initializing native brush with null." ); 
            SetNativeBrushInternal( nativeBrush ); 
        }
 
        /// 
        /// 
        ///    Creates an exact copy of this .
        ///  
        [ResourceExposure(ResourceScope.Process)]
        [ResourceConsumption(ResourceScope.Process)] 
        public override object Clone() { 
            IntPtr cloneBrush = IntPtr.Zero;
 
            int status = SafeNativeMethods.Gdip.GdipCloneBrush(new HandleRef(this, this.NativeBrush), out cloneBrush);

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

            return new PathGradientBrush(cloneBrush ); 
        } 

        /** 
         * Set/get center color attributes
         */
        /// 
        ///  
        ///    Gets or sets the color at the center of the
        ///    path gradient. 
        ///  
        public Color CenterColor
        { 
            get {
                int argb;

                int status = SafeNativeMethods.Gdip.GdipGetPathGradientCenterColor(new HandleRef(this, this.NativeBrush), out argb); 

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

                return Color.FromArgb(argb); 
            }

            set {
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientCenterColor(new HandleRef(this, this.NativeBrush), value.ToArgb()); 

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

        /**
         * Get/set colors
         * !! NOTE: We do not have methods for GetSurroundColor or SetSurroundColor, 
         *    May need to add usage of Collection class
         */ 
 
        private void _SetSurroundColors(Color[] colors)
        { 
            int count;

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientSurroundColorCount(new HandleRef(this, this.NativeBrush),
                                                                       out count); 

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

            if ((colors.Length > count) || (count <= 0)) 
                throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.InvalidParameter);

            count = colors.Length;
            int[] argbs = new int[count]; 
            for (int i=0; i 
        /// 
        ///     
        ///       Gets or sets an array of colors that
        ///       correspond to the points in the path this  fills.
        ///    
        ///  
        public Color[] SurroundColors
        { 
            get { return _GetSurroundColors();} 
            set { _SetSurroundColors(value);}
        } 

       /**
         * Set/get center point
         */ 
        /// 
        ///  
        ///     
        ///       Gets or sets the center point of the path
        ///       gradient. 
        ///    
        /// 
        public PointF CenterPoint
        { 
            get {
                GPPOINTF point = new GPPOINTF(); 
 
                int status = SafeNativeMethods.Gdip.GdipGetPathGradientCenterPoint(new HandleRef(this, this.NativeBrush), point);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);

                return point.ToPoint(); 
            }
 
            set { 
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientCenterPoint(new HandleRef(this, this.NativeBrush), new GPPOINTF(value));
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
            }
        } 

        /** 
         * Get source rectangle 
         */
        private RectangleF _GetRectangle() { 
            GPRECTF rect = new GPRECTF();

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientRect(new HandleRef(this, this.NativeBrush), ref rect);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
 
            return rect.ToRectangleF();
        } 

        /// 
        /// 
        ///     
        ///       Gets a bounding rectangle for this .
        ///     
        ///  
        public RectangleF Rectangle
        { 
            get { return _GetRectangle();}
        }

        /** 
         * Set/get blend factors
         */ 
        private Blend _GetBlend() { 
            Blend blend;
 
            // Figure out the size of blend factor array
            int retval = 0;
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientBlendCount(new HandleRef(this, this.NativeBrush), out retval);
 
            if (status != SafeNativeMethods.Gdip.Ok) {
                throw SafeNativeMethods.Gdip.StatusException(status); 
            } 

            // Allocate temporary native memory buffer 

            int count = retval;

            IntPtr factors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;
 
            try { 
                factors = Marshal.AllocHGlobal(4*count);
                positions = Marshal.AllocHGlobal(4*count); 

                // Retrieve horizontal blend factors

                status = SafeNativeMethods.Gdip.GdipGetPathGradientBlend(new HandleRef(this, this.NativeBrush), factors, positions, count); 

                if (status != SafeNativeMethods.Gdip.Ok) { 
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                }
 
                // Return the result in a managed array

                blend = new Blend(count);
 
                Marshal.Copy(factors, blend.Factors, 0, count);
                Marshal.Copy(positions, blend.Positions, 0, count); 
 
            }
            finally { 
                if( factors != IntPtr.Zero ) {
                    Marshal.FreeHGlobal(factors);
                }
                if( positions != IntPtr.Zero ) { 
                    Marshal.FreeHGlobal(positions);
                } 
            } 

            return blend; 
        }

        private void _SetBlend(Blend blend) {
            // Allocate temporary native memory buffer 
            // and copy input blend factors into it.
 
            int count = blend.Factors.Length; 

            IntPtr factors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;

            try {
                factors = Marshal.AllocHGlobal(4*count); 
                positions = Marshal.AllocHGlobal(4*count);
 
                Marshal.Copy(blend.Factors, 0, factors, count); 
                Marshal.Copy(blend.Positions, 0, positions, count);
 
                // Set blend factors

                int status = SafeNativeMethods.Gdip.GdipSetPathGradientBlend(new HandleRef(this, this.NativeBrush), new HandleRef(null, factors), new HandleRef(null, positions), count);
 
                if (status != SafeNativeMethods.Gdip.Ok) {
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                } 
            }
            finally { 
                if( factors != IntPtr.Zero ) {
                    Marshal.FreeHGlobal(factors);
                }
                if( positions != IntPtr.Zero ) { 
                    Marshal.FreeHGlobal(positions);
                } 
            } 
        }
 
        /// 
        /// 
        ///    
        ///       Gets or sets a  that specifies positions and factors 
        ///       that define a custom falloff for the gradient.
        ///     
        ///  
        public Blend Blend
        { 
            get { return _GetBlend();}
            set { _SetBlend(value);}
        }
 
        /*
         * SigmaBlend & LinearBlend 
         */ 

        ///  
        /// 
        ///    
        ///       Creates a gradient falloff based on a bell-shaped curve.
        ///     
        /// 
        public void SetSigmaBellShape(float focus) 
        { 
            SetSigmaBellShape(focus, (float)1.0);
        } 

        /// 
        /// 
        ///     
        ///       Creates a gradient falloff based on a bell-shaped curve.
        ///     
        ///  
        public void SetSigmaBellShape(float focus, float scale)
        { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientSigmaBlend(new HandleRef(this, this.NativeBrush), focus, scale);

            if (status != SafeNativeMethods.Gdip.Ok)
                 throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        ///  
        /// 
        ///     
        ///       Creates a triangular gradient.
        ///    
        /// 
        public void SetBlendTriangularShape(float focus) 
        {
            SetBlendTriangularShape(focus, (float)1.0); 
        } 

        ///  
        /// 
        ///    
        ///       Creates a triangular gradient.
        ///     
        /// 
        public void SetBlendTriangularShape(float focus, float scale) 
        { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientLinearBlend(new HandleRef(this, this.NativeBrush), focus, scale);
 
            if (status != SafeNativeMethods.Gdip.Ok)
                 throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /*
         * Preset Color Blend 
         */ 

        private ColorBlend _GetInterpolationColors() { 
            ColorBlend blend;

            // Figure out the size of blend factor array
            int retval = 0; 
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientPresetBlendCount(new HandleRef(this, this.NativeBrush), out retval);
 
            if (status != SafeNativeMethods.Gdip.Ok) { 
                throw SafeNativeMethods.Gdip.StatusException(status);
            } 

            // If retVal is 0, then there is nothing to marshal.
            // In this case, we'll return an empty ColorBlend...
            // 
            if (retval == 0) {
                return new ColorBlend(); 
            } 

            // Allocate temporary native memory buffer 

            int count = retval;

            IntPtr colors = IntPtr.Zero; 
            IntPtr positions = IntPtr.Zero;
 
            try { 
                colors = Marshal.AllocHGlobal(4*count);
                positions = Marshal.AllocHGlobal(4*count); 

                // Retrieve horizontal blend factors

                status = SafeNativeMethods.Gdip.GdipGetPathGradientPresetBlend(new HandleRef(this, this.NativeBrush), colors, positions, count); 

                if (status != SafeNativeMethods.Gdip.Ok) { 
                    throw SafeNativeMethods.Gdip.StatusException(status); 
                }
 
                // Return the result in a managed array

                blend = new ColorBlend(count);
 
                int[] argb = new int[count];
                Marshal.Copy(colors, argb, 0, count); 
                Marshal.Copy(positions, blend.Positions, 0, count); 

                // copy ARGB values into Color array of ColorBlend 
                blend.Colors = new Color[argb.Length];

                for (int i=0; i 
        /// 
        ///     
        ///       Gets or sets a  that defines a multi-color linear 
        ///       gradient.
        ///     
        /// 
        public ColorBlend InterpolationColors
        {
            get { return _GetInterpolationColors(); } 
            set { _SetInterpolationColors(value); }
        } 
 
        /**
         * Set/get brush transform 
         */
        private void _SetTransform(Matrix matrix) {
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int status = SafeNativeMethods.Gdip.GdipSetPathGradientTransform(new HandleRef(this, this.NativeBrush), new HandleRef(matrix, matrix.nativeMatrix)); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        private Matrix _GetTransform() {
            Matrix matrix = new Matrix(); 

            int status = SafeNativeMethods.Gdip.GdipGetPathGradientTransform(new HandleRef(this, this.NativeBrush), new HandleRef(matrix, matrix.nativeMatrix)); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 

            return matrix;
        }
 
        /// 
        ///  
        ///     
        ///       Gets or sets a  that defines a local geometrical
        ///       transform for this . 
        ///    
        /// 
        public Matrix Transform
        { 
            get { return _GetTransform();}
            set { _SetTransform(value);} 
        } 

        ///  
        /// 
        ///    
        ///       Resets the  property to
        ///       identity. 
        ///    
        ///  
        public void ResetTransform() { 
            int status = SafeNativeMethods.Gdip.GdipResetPathGradientTransform(new HandleRef(this, this.NativeBrush));
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }
 
        /// 
        ///  
        ///     
        ///       Multiplies the  that represents the local geometrical
        ///       transform of this  by the specified  by prepending the specified . 
        ///    
        /// 
        public void MultiplyTransform(Matrix matrix)
        { 
            MultiplyTransform(matrix, MatrixOrder.Prepend);
        } 
 
        /// 
        ///  
        ///    
        ///       Multiplies the  that represents the local geometrical
        ///       transform of this  by the specified  in the specified order.
        ///     
        /// 
        public void MultiplyTransform(Matrix matrix, MatrixOrder order) 
        { 
            if (matrix == null)
                throw new ArgumentNullException("matrix"); 

            int status = SafeNativeMethods.Gdip.GdipMultiplyPathGradientTransform(new HandleRef(this, this.NativeBrush),
                                                new HandleRef(matrix, matrix.nativeMatrix),
                                                order); 

            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        /// 
        /// 
        ///    
        ///       Translates the local geometrical transform by the specified dimmensions. This 
        ///       method prepends the translation to the transform.
        ///     
        ///  
        public void TranslateTransform(float dx, float dy)
        { 
            TranslateTransform(dx, dy, MatrixOrder.Prepend);
        }

        ///  
        /// 
        ///     
        ///       Translates the local geometrical transform by the specified dimmensions in 
        ///       the specified order.
        ///     
        /// 
        public void TranslateTransform(float dx, float dy, MatrixOrder order)
        {
            int status = SafeNativeMethods.Gdip.GdipTranslatePathGradientTransform(new HandleRef(this, this.NativeBrush), 
                                                dx, dy, order);
 
            if (status != SafeNativeMethods.Gdip.Ok) 
                throw SafeNativeMethods.Gdip.StatusException(status);
        } 

        /// 
        /// 
        ///     
        ///       Scales the local geometric transform by the specified amounts. This method
        ///       prepends the scaling matrix to the transform. 
        ///     
        /// 
        public void ScaleTransform(float sx, float sy) 
        {
            ScaleTransform(sx, sy, MatrixOrder.Prepend);
        }
 
        /// 
        ///  
        ///     
        ///       Scales the local geometric transform by the specified amounts in the
        ///       specified order. 
        ///    
        /// 
        public void ScaleTransform(float sx, float sy, MatrixOrder order)
        { 
            int status = SafeNativeMethods.Gdip.GdipScalePathGradientTransform(new HandleRef(this, this.NativeBrush),
                                                sx, sy, order); 
 
            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }

        /// 
        ///  
        ///    
        ///       Rotates the local geometric transform by the specified amount. This method 
        ///       prepends the rotation to the transform. 
        ///    
        ///  
        public void RotateTransform(float angle)
        {
            RotateTransform(angle, MatrixOrder.Prepend);
        } 

        ///  
        ///  
        ///    
        ///       Rotates the local geometric transform by the specified amount in the 
        ///       specified order.
        ///    
        /// 
        public void RotateTransform(float angle, MatrixOrder order) 
        {
            int status = SafeNativeMethods.Gdip.GdipRotatePathGradientTransform(new HandleRef(this, this.NativeBrush), 
                                                angle, order); 

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

        /** 
         * Set/get brush focus scales
         */ 
        ///  
        /// 
        ///    Gets or sets the focus point for the 
        ///    gradient falloff.
        /// 
        public PointF FocusScales
        { 
            get {
                float[] scaleX = new float[] { 0.0f }; 
                float[] scaleY = new float[] { 0.0f }; 

                int status = SafeNativeMethods.Gdip.GdipGetPathGradientFocusScales(new HandleRef(this, this.NativeBrush), scaleX, scaleY); 

                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
 
                return new PointF(scaleX[0], scaleY[0]);
            } 
            set { 
                int status = SafeNativeMethods.Gdip.GdipSetPathGradientFocusScales(new HandleRef(this, this.NativeBrush), value.X, value.Y);
 
                if (status != SafeNativeMethods.Gdip.Ok)
                    throw SafeNativeMethods.Gdip.StatusException(status);
            }
        } 

        /** 
         * Set/get brush wrapping mode 
         */
        private void _SetWrapMode(WrapMode wrapMode) { 
            int status = SafeNativeMethods.Gdip.GdipSetPathGradientWrapMode(new HandleRef(this, this.NativeBrush), (int) wrapMode);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status); 
        }
 
        private WrapMode _GetWrapMode() { 
            int mode = 0;
 
            int status = SafeNativeMethods.Gdip.GdipGetPathGradientWrapMode(new HandleRef(this, this.NativeBrush), out mode);

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

            return (WrapMode) mode; 
        } 

        ///  
        /// 
        ///    
        ///       Gets or sets a  that indicates the wrap mode for this
        ///    . 
        ///    
        ///  
        public WrapMode WrapMode 
        {
            get { 
                return _GetWrapMode();
            }
            set {
                //validate the WrapMode enum 
                if (!ClientUtils.IsEnumValid(value, (int)value, (int)WrapMode.Tile, (int)WrapMode.Clamp))
                { 
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(WrapMode)); 
                }
 
                _SetWrapMode(value);
            }
        }
    } 

} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

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