GradientFill.SetGradient

GradientFill.SetGradient method

Set the gradient fill type and direction.

public void SetGradient(GradientFillType type, double angle, GradientDirectionType direction)
ParameterTypeDescription
typeGradientFillTypeGradient fill type.
angleDoubleThe angle. Only applies for GradientFillType.Linear.
directionGradientDirectionTypeThe direction type. Only applies for GradientFillType.Radial and GradientFillType.Rectangle.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Drawing;
    using System;

    public class GradientFillMethodSetGradientWithGradientFillTypeDoubleGradientDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            
            // Add a shape to apply gradient fill
            Shape shape = worksheet.Shapes.AddRectangle(0, 0, 100, 100, 200, 100);

            try
            {
                // Get the shape's fill gradient
                GradientFill gradientFill = shape.Fill.GradientFill; // Fixed property name from Gradient to GradientFill
                if (gradientFill == null)
                {
                    Console.WriteLine("Cannot set gradient: The shape's fill is not a GradientFill.");
                    return;
                }

                // Call SetGradient with specific parameters: Linear fill, 90 degrees, FromCenter direction
                gradientFill.SetGradient(GradientFillType.Linear, 90.0, GradientDirectionType.FromCenter);
                
                Console.WriteLine("SetGradient method executed successfully with parameters (GradientFillType.Linear, 90.0, GradientDirectionType.FromCenter)");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing SetGradient method: {ex.Message}");
            }
            
            // Save the workbook to demonstrate the effect
            workbook.Save("GradientFillSetGradientDemo.xlsx");
        }
    }
}

See Also