SVG icon indicating copy to clipboard operation
SVG copied to clipboard

BUG : OutOfMemory Exception with Gradients

Open MCKLMT opened this issue 10 years ago • 5 comments

Hi,

When trying to draw a SVG, I receive an out of memory exception with this stack trace:

à System.Drawing.Drawing2D.LinearGradientBrush..ctor(PointF point1, PointF point2, Color color1, Color color2) à Svg.SvgLinearGradientServer.GetBrush(SvgVisualElement renderingElement, SvgRenderer renderer, Single opacity) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\Painting\SvgLinearGradientServer.cs:ligne 107 à Svg.SvgVisualElement.RenderFill(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\Basic Shapes\SvgVisualElement.cs:ligne 141 à Svg.SvgVisualElement.Render(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\Basic Shapes\SvgVisualElement.cs:ligne 119 à Svg.SvgElement.RenderChildren(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\SvgElement.cs:ligne 653 à Svg.SvgGroup.Render(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\Document Structure\SvgGroup.cs:ligne 71 à Svg.SvgElement.RenderChildren(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\SvgElement.cs:ligne 653 à Svg.SvgElement.Render(SvgRenderer renderer) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\SvgElement.cs:ligne 641 à Svg.SvgDocument.Draw(Bitmap bitmap) dans d:\TFS\GiresV2\Development-v2\Sources\Svg\SvgDocument.cs:ligne 464 à Svg.SvgDocument.Draw() dans d:\TFS\GiresV2\Development-v2\Sources\Svg\SvgDocument.cs:ligne 437

Knew problem?

MCKLMT avatar Aug 21 '14 13:08 MCKLMT

This is a known GDI issue with very small values (gradients in this case) - the exception is misleading as it is no real out of memory error. I think this can be fixed by adding checks for small values - I may look into this some time later.

mrbean-bremen avatar Dec 17 '18 18:12 mrbean-bremen

927050448_01

I have encountered a similar issue, for the above SVG, In my case, some of the linearGradient has 0 height (y1 == y2)

<linearGradient x1="252.49" y1="651.83" x2="559.16" y2="651.83" xlink:href="#linear-gradient" gradientTransform="translate(-53.42, -414.64)" id="linear-gradient-2"/>

The call stack is

System.OutOfMemoryException: 'Out of memory.'
This exception was originally thrown at this call stack:
    System.Drawing.SafeNativeMethods.Gdip.CheckStatus(int)
    System.Drawing.Drawing2D.LinearGradientBrush.LinearGradientBrush(System.Drawing.PointF, System.Drawing.PointF, System.Drawing.Color, System.Drawing.Color)
    Svg.SvgLinearGradientServer.GetBrush(Svg.SvgVisualElement, Svg.ISvgRenderer, float, bool)
    Svg.SvgDeferredPaintServer.GetBrush(Svg.SvgVisualElement, Svg.ISvgRenderer, float, bool)
    Svg.SvgVisualElement.RenderFill(Svg.ISvgRenderer)
    Svg.SvgVisualElement.RenderFillAndStroke(Svg.ISvgRenderer)
    Svg.SvgVisualElement.RenderInternal(Svg.ISvgRenderer, System.Action<Svg.ISvgRenderer>)
    Svg.SvgVisualElement.RenderInternal(Svg.ISvgRenderer, bool)
    Svg.SvgVisualElement.Render(Svg.ISvgRenderer)
    Svg.SvgRectangle.Render(Svg.ISvgRenderer)

I tried to add a very small value to the y2, then the exception is no longer thrown, however the gradient is not rendered correctly. For example, converted PNG, which the gradient is significantly different than the actual svg rendered in browser (at the beginning of this post) image

@mrbean-bremen do you have any plans to address this? Or maybe has a better solution than simply add a small value to make brush height not 0?

guoyu-wang avatar Mar 31 '22 04:03 guoyu-wang

@gwang-msft I want to compare it with major browsers, so please upload complete SVG file.

H1Gdev avatar Mar 31 '22 06:03 H1Gdev

@gwang-msft I want to compare it with major browsers, so please upload complete SVG file.

@H1Gdev, the svg file was uploaded in my previous post, here is the direct link https://user-images.githubusercontent.com/62914304/160978648-3bc1935e-e9ac-4040-a8dc-5d9a07210654.svg

Thanks!

guoyu-wang avatar Mar 31 '22 07:03 guoyu-wang

The PR fixes the issue reported by @guoyu-wang, which is unrelated to the original problem, which remains. Reopen.

mrbean-bremen avatar Apr 15 '22 11:04 mrbean-bremen

Is there any update on this? We started to use the SVG.net library on a new project and encountered the same problem, as our whole icon collection contains gradients.

Hurricane31337 avatar Feb 03 '23 09:02 Hurricane31337

Update: I applied the proposed changes from https://github.com/svg-net/SVG/issues/1008 and the problem seems to be fixed.

Hurricane31337 avatar Feb 03 '23 12:02 Hurricane31337

Yes, I think this (ignoring small differences) is the way to fix this. There has been no update on this - the issue has obviously been forgotten - but if somebody could pull a PR with these changes together, it would be nice.

It is unlikely that I will get to this myself in the near future, as I'm mostly busy with other stuff and have not been working on SVG for a long time now. Though I may have a shot some time later on if nobody else gets to it...

mrbean-bremen avatar Feb 03 '23 19:02 mrbean-bremen

I created a pull request for this fix, as mentioned: https://github.com/svg-net/SVG/pull/1035

Hurricane31337 avatar Feb 06 '23 09:02 Hurricane31337