none
PathGradientBrush and the OutofMemory Exception RRS feed

  • Question

  • I've recently been using the PathGradientBrush at work, and I've found that when it goes to create a new instance it throws an OutOfMemoryException every time.  I've tried a clean reboot and it still does it, I've tried making sure barely anything is in memory and it still does it.

    The worst of it is, I've tried to execute the same code on my home PC and it works fine.

    Anyone got any ideas as to what might be causing the OutOfMemoryException at work?

    Monday, August 21, 2006 7:43 AM

All replies

  • I don't know why the same code should work at home, but I have had a similar problem (OutOfMemoryException) when trying to instantiate a LinearGradientBrush.

    It only happened when I passed a 'non-gradient' to the constructor. The constructor I used wants points to define the start and end of the gradient. If the two points were the same (so there is no gradient), then I got the OutOfMemoryException.

    For example:

    PointF pt1 = new PointF(100,100);
    PointF pt2 = new PointF(100,100); // i.e. pt1 equals pt2
    LinearGradientBrush lgBrush = new LinearGradientBrush(pt1, pt2, Color.Red, Color.Blue);

    gives the OutOfMemoryException. If pt1 and pt2 differ, everything's fine.

    I tried the PathGradientBrush very briefly and got the same message as you when I tried to instantiate a new instance with the PathGradientBrush(GraphicsPath path) constructor, if the path passed to it was either empty or if it had either no height or no width.

    For example:

    GraphicsPath gp = new GraphicsPath();
    gp.AddLine(new Point(0, 0), new Point(0, 0)); // no height or width.
    PathGradientBrush gBrush = new PathGradientBrush(gp);

    gives OutOfMemoryException

    Also, substituting ...

    gp.AddLine(new Point(0,0), new Point(100,0)) ; // horizontal line but path has no 'height'
    or, gp.AddLine(new Point(0,0), new Point(0,100)); // vertical line but path has no width

    also gave the exception,

    while, gp.AddLine(new Point(0,0), new Point(100,100); // diagonal line, so path has a width and a height

    worked fine.

    Don't know whether this explains your OutOfMemoryException, but hope it helps.




    Wednesday, August 23, 2006 12:40 AM
  • Thanks very much for that. 

    It did help, and the exception isn't thrown when instantiating the PathGradientBrush.  Instead, I now get an ArgumentException when I try to apply a new ColorBlend to the InterpolationColors.  I've checked to make sure it's correct.  I have 2 colours, and the positions are set at 0.0 and 0.8.  Now for some unknown reason this is classed as an invalid parameter?!

    Is it just me or does PathGradientBrush seem buggy?

    Thursday, August 24, 2006 8:11 AM