Click here to Skip to main content
14,693,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using W10/64 and VS 2019 Community, c#
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.

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 );   


What I have tried:

I have tried setting myColors to fixed values like this, { Color.Red, Color.White, Color.Red };
I have tried changing linGrBrush to fixed colors like Color.Black and Color.White.
I have tried all of the examples I could find.
Error written by VS is always the same as above.
If I breakpoint on the problem line VS flags an exception but inspection of colorBlend does not show any errors.
Posted
Updated 22-Apr-20 7:46am
v2
Comments
Richard MacCutchan 22-Apr-20 11:33am
   
I just tried that code and it works correctly!
Member 7807309 23-Apr-20 17:04pm
   
Thanks Richard,
The code as posted is exactly what I am executing. Although it is in a loop where I am changing the fillColor it fails on the first time through the loop.
Thanks for running the code.
Jim
Richard MacCutchan 24-Apr-20 2:56am
   
Well that is only part of the code that you are running, so something else must be happening.

1 solution

Works for me too in VS2019 on Win10 .NET 4.72.
Slightly changed, using a Windows Form with a Panel:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
private void panel1_Paint(object sender, PaintEventArgs e)
{
    Color fillColor = Color.Blue;
    Rectangle myRectangle = new Rectangle(0, 0, this.panel1.Width, this.panel1.Height);

    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);
    linGrBrush.InterpolationColors = colorBlend;
    e.Graphics.FillRectangle(linGrBrush, myRectangle);
}
   
Comments
BillWoodruff 23-Apr-20 2:52am
   
+5
Member 7807309 24-Apr-20 14:47pm
   
Thanks all for the help. The problem must lie elsewhere. At least I know there is nothing wrong with the posted code.

Jim

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900