ColorBlend problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ImaCoder5531
    New Member
    • Apr 2020
    • 2

    ColorBlend problem

    Date: 4/21/20
    Using W10/64 and VS 2019 Community.
    I am trying to do a simple 3 color horizontal gradient fill. I was able to trap the error but not find out exactly the problem. A breakpoint on the problem line indicated colorBlend to be correct. Here is the code. I would appreciate any help with this.

    Code:
    Rectangle myRectangle = new Rectangle ( x3, y1, x4 - x3, y2 - y1 + 1 );
    // fillColor is any arbitrary Color.  Eg Color.Blue
    Color[] myColors        = { fillColor, Color.White, fillColor };
    float[] myPositions     = { 0f, 0.5f, 1f };
    ColorBlend colorBlend   = new ColorBlend ();
    colorBlend.Colors       = myColors;
    colorBlend.Positions    = myPositions;
    
    LinearGradientBrush linGrBrush = 
       new LinearGradientBrush ( myRectangle,
       fillColor,
       fillColor, LinearGradientMode.Horizontal );
    
    // failure is at the following line.  VS gives this error:
    // System.ArgumentException: 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. at
    // System.Drawing.Drawing2D.LinearGradientBrush._GetI
    // nterpolationColors() indicating the last element in 
    // the array. at System.Drawing.Drawing2D.LinearGradientBrush
    // ._GetInterpolationColors()        
    
    linGrBrush.InterpolationColors = colorBlend;  // error here
    
    graphicsObject.FillRectangle ( linGrBrush, myRectangle );
    Last edited by ImaCoder5531; Apr 21 '20, 05:41 PM. Reason: misspelled rectangle
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Can you provide 'complete' example? I do mean some code which produces an error on some line, but not some code with comment that the error happens there....

    Comment

    • ImaCoder5531
      New Member
      • Apr 2020
      • 2

      #3
      Hello Luuk,

      Thanks for your reply. Two other experts have reported the posted code is error free so my problem must be elsewhere. Thank you for your time.

      Jim

      Comment

      Working...