By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

ColorBlend problem

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. Rectangle myRectangle = new Rectangle ( x3, y1, x4 - x3, y2 - y1 + 1 );
  2. // fillColor is any arbitrary Color.  Eg Color.Blue
  3. Color[] myColors        = { fillColor, Color.White, fillColor };
  4. float[] myPositions     = { 0f, 0.5f, 1f };
  5. ColorBlend colorBlend   = new ColorBlend ();
  6. colorBlend.Colors       = myColors;
  7. colorBlend.Positions    = myPositions;
  8.  
  9. LinearGradientBrush linGrBrush = 
  10.    new LinearGradientBrush ( myRectangle,
  11.    fillColor,
  12.    fillColor, LinearGradientMode.Horizontal );
  13.  
  14. // failure is at the following line.  VS gives this error:
  15. // System.ArgumentException: Property must be set to
  16. // a valid ColorBlend object to use interpolation
  17. // colors. ColorBlend objects must be constructed
  18. // with the same number of positions and color
  19. // values. Positions must be between 0.0 and 1.0, 1.0
  20. // indicating the last element in the array. at
  21. // System.Drawing.Drawing2D.LinearGradientBrush._GetI
  22. // nterpolationColors() indicating the last element in 
  23. // the array. at System.Drawing.Drawing2D.LinearGradientBrush
  24. // ._GetInterpolationColors()        
  25.  
  26. linGrBrush.InterpolationColors = colorBlend;  // error here
  27.  
  28. graphicsObject.FillRectangle ( linGrBrush, myRectangle );  
  29.  
4 Weeks Ago #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,043
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....
4 Weeks Ago #2

P: 2
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
4 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.