SVG
SVG copied to clipboard
BUG : OutOfMemory Exception with Gradients
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?
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.
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)
@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?
@gwang-msft I want to compare it with major browsers, so please upload complete SVG file.
@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!
The PR fixes the issue reported by @guoyu-wang, which is unrelated to the original problem, which remains. Reopen.
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.
Update: I applied the proposed changes from https://github.com/svg-net/SVG/issues/1008 and the problem seems to be fixed.
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...
I created a pull request for this fix, as mentioned: https://github.com/svg-net/SVG/pull/1035