6

Here's an example:

public MainForm()
    {
        InitializeComponent();

          LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,100,100),Color.Blue, Color.White,angle:0);
          brush.WrapMode = WrapMode.Tile; // OK
          brush.WrapMode = WrapMode.Clamp; // Causes Unhandled exception alert, offering break
    }

In the VS2008 output window this shows:

A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll Additional information: Parameter is not valid.

(pic https://i.stack.imgur.com/51a43.png)

This is on Windows 7.

Documentation here https://msdn.microsoft.com/en-us/library/vstudio/system.drawing.drawing2d.lineargradientbrush.wrapmode(v=vs.90).aspx

confirms LinearGradientBrush.WrapMode accepts a WrapMode

"Gets or sets a WrapMode enumeration that indicates the wrap mode for this LinearGradientBrush."

and this https://msdn.microsoft.com/en-us/library/vstudio/system.drawing.drawing2d.wrapmode(v=vs.90).aspx

confirms that WrapMode.Clamp is valid for gradient:

"Clamp The texture or gradient is not tiled."

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
ChrisJJ
  • 2,191
  • 1
  • 25
  • 38
  • I can confirm this with VS15 on W8.1. This looks like a bug, as `Clamp` ie untiled continuation with, probably the last color would be quite useful. You can try to workaround with `InterpolationColors` but that requires some knowledge of the sizes to fill.. – TaW Oct 20 '15 at 09:03
  • Thanks. Please enter your response as an Answer so I can accept it. – ChrisJJ Oct 20 '15 at 15:16
  • 2
    That's an odd error. You can try creating a bitmap tile of your LinearGradientBrush and then use that bitmap in a TextureBrush, which does not throw an error when it's WrapMode is set for Clamp. – LarsTech Oct 20 '15 at 16:35

2 Answers2

3

I can confirm this with VS2015 on Windows 8.1.

This looks like a bug to me, as Clamp i.e. untiled continuation with, probably the last color would be quite useful.

You can try to workaround with InterpolationColors but that requires some knowledge of the sizes to fill..

See here and espcially here for code examples for using InterpolationColors

Update: Lars' idea looks also very interesting: Create a tile with the LinearGradientBrush and then use it with a TextureBrush..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
0

For me it works fine to scale the vector and repeat the end colors, using SetInterpolationColors (C++). I leave a space of 10 * VectorSize at both ends and normalize the stops.

GridientClamp

enter image description here

frang
  • 69
  • 5