C#-APP: Problem with LinearGradientBrush

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cblackburn
    New Member
    • Sep 2008
    • 3

    C#-APP: Problem with LinearGradientBrush

    Hi all,

    I am in the situation where I have a GraphicsPath that I want to draw onto a System.Drawing. Graphics. I want the line to be coloured in a gradient from one end to the other. I have tried to implement this using a LinearGradientB rush but that does not follow the path of the line, it simply creates a one directional gradient and the line is rendered ontop of that.

    Does anyone have a suggestion about how to go about implementing this or am I missing some obvious subclass of System.Drawing. Brush that does this for me :-D

    Btw, if it matters, This is being done in c# to an ASP.NET Application on .NET 2.0.

    Thanks

    Chris
  • scriptizer
    New Member
    • Aug 2008
    • 2

    #2
    can you post the code please so i can help you?

    Comment

    • cblackburn
      New Member
      • Sep 2008
      • 3

      #3
      Ok,

      The code is at a pretty primitive state at the moment so I'll get back to you once I've done some more work today but what I'm basically trying to achieve is exemplified in the image below. I've not found anyone else on the internet who's managed to do it so far so I'm not looking forward to fixing this one :-(



      Chris

      Comment

      • cblackburn
        New Member
        • Sep 2008
        • 3

        #4
        C#-APP: Problem with LinearGradientB rush

        Hi all,

        I have come up against an interesting problem with LinearGradientB rush. Could someone tell me why the following code crashes? This is on any .NET application in C# (Web and Console) running .NET 2.0

        Thanks

        Chris

        This Exception is raised on the last line of code (Line 30)
        Code:
        System.ArgumentException was unhandled
          Message="Property must be set to a valid ColorBlend object to use interpolation colors. ColorBlend objects must be constructed with the same number of positions and color values. Positions must be between 0.0 and 1.0, 1.0 indicating the last element in the array."
          Source="System.Drawing"
          StackTrace:
               at System.Drawing.Drawing2D.LinearGradientBrush._GetInterpolationColors()
               at System.Drawing.Drawing2D.LinearGradientBrush.get_InterpolationColors()
               at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 30
               at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
               at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
               at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
               at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
               at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
               at System.Threading.ThreadHelper.ThreadStart()
          InnerException:
        This is the source code to the program:-
        Code:
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        
        namespace ConsoleApplication1
        {
            class Program
            {
                static void Main(string[] args)
                {
                    LinearGradientBrush brush = new LinearGradientBrush(new Point(1, 1),
                                                                        new Point(1, 2),
                                                                        Color.Black,
                                                                        Color.Blue);
        
                    Color[] colors = { Color.Red, Color.Green, Color.Blue };
                    float[] positions = { 0.0f, 0.5f, 1.0f };
                    
                    ColorBlend blend = new ColorBlend();
                    blend.Colors = colors;
                    blend.Positions = positions;
        
                    brush.InterpolationColors = blend;
        
                    Pen pen = new Pen(brush);
        
                    ColorBlend getBlend = (pen.Brush as LinearGradientBrush).InterpolationColors;
                }
            }
        }

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Since this was on the same topic as your other thread, I have merged them.

          MODERATOR

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well a linear gradiant would be just that. A gradiant applied linearly.
            So if it goes top->bottom, the top x% would be a color, then the next y% would be a color and so on and so on. For the full width of your canvas.
            It works the same for going left->right, with the left x% being a color and moving right.
            If you want to draw a line that zigzags around with different colors, I don't think that brush is correct.

            If it helps, generally with a gradiant, the color changes are applied to the ENTIRE canvas, then "covered" with an empty color (white?) and where you draw your lines could be though of as "erasing" that empty(white) color.

            Comment

            Working...