SVG icon indicating copy to clipboard operation
SVG copied to clipboard

Convert SVG to WMF on Linux/mono.

Open mustelan opened this issue 5 years ago • 5 comments

Description

Some time ago I found code snippet (using this library) convert SVG to WMF:

using (var memoryStream = new MemoryStream())
using (var image = new Bitmap(100,100))
using (var bufferGraphics = Graphics.FromImage(image))
using (var metafile = new Metafile(memoryStream, bufferGraphics.GetHdc()))
{
    using (var graphics = Graphics.FromImage(metafile))
    {
        svgDocument.Draw(graphics);
    }
    return (memoryStream.ToArray(), metafile.Width, metafile.Height);
}

I'm not exactly sure how this code works, but works and produces correct WMF :)

Now, I migrate from Windows environment to Linux/MONO and when I use version 2.4.3 I get an exception:

System.TypeInitializationException : The type initializer for 'Svg.SvgDocument' threw an exception.
  ----> System.EntryPointNotFoundException : GetDC

I understand in this case version 2.4.3 is strictly related with Windows GDI library and it's not possible to work with mono.

But when I use prerelease version (3.0.20-g594db9696a) there is no exception, but produced WMF is empty (0 bytes length). There is any possibility to produce correct WMF file on mono with this library?

Example

sample_svg.txt

Used Versions

  • version 2.4.3
  • 3.0.20-g594db9696a

mustelan avatar Aug 02 '19 08:08 mustelan

Can you check what happens if you try to create a PNG on you system? I am wondering whether the problem is that mono is not able to process any image or only refuses to render WMF (since I would assume that is a very specific Windows thingy).

I am also a bit puzzled by the fact that you are writing a bitmap to a vector image (WMF is a vector image), not that it's really any of my business, but I am very curious why you want to do that. Perhaps you don't need to go the WMF route :)

gvheertum avatar Aug 04 '19 12:08 gvheertum

I am also a bit puzzled by the fact that you are writing a bitmap to a vector image (WMF is a vector image), not that it's really any of my business, but I am very curious why you want to do that. Perhaps you don't need to go the WMF route :)

My code based on this piece of code: https://github.com/vvvv/SVG/blob/54ef89bb8f63c2d47c4069e3303cb01f81cad31d/Tests/Svg.UnitTests/MetafileRenderingTest.cs#L29

And it works - I get vector WMF image (which I can ungroup, change colors and so on).

Can you check what happens if you try to create a PNG on you system? I am wondering whether the problem is that mono is not able to process any image or only refuses to render WMF (since I would assume that is a very specific Windows thingy)

I will check and let you know.

mustelan avatar Aug 08 '19 09:08 mustelan

My code based on this piece of code:

https://github.com/vvvv/SVG/blob/54ef89bb8f63c2d47c4069e3303cb01f81cad31d/Tests/Svg.UnitTests/MetafileRenderingTest.cs#L29

Can you check if the UnitTest runs correctly on your configuration? If the UnitTest works well, the problem might be in your code or in the SVG that is being drawn. If the UnitTest fails, the problem would most likely be in the library itself.

A small run of the code in the test learned me that MacOs is also not playing nice with this specific code/unittest (it will complain that the object is in use when trying to release the handle). The code seems to work with handles and I can image these might cause issues in the GDI+ wrappers/substitutions we use for Mac and Linux. I guess working with handles and a replacement of the GDI+ wrapper don't play along very nicely.

Mono states that the Handle function in their libgdipus are not (fully) implemented (https://github.com/mono/libgdiplus/blob/master/TODO). And since the exception states that GetDC has no entry point, I guess the call will have something to do with the GetHdc function in the bitmap, which uses handles.

So for a non Windows implementation, I think this code construct might not work (yet) and you might consider changing it to a different format or an implementation that is not using handles.

I will check and let you know. Great, thanks.

gvheertum avatar Aug 08 '19 13:08 gvheertum

@mustelan , did you get around testing/playing some more with this?

gvheertum avatar Oct 01 '19 08:10 gvheertum

Few days ago I updated: this library to the newset version, System.Drawing.Common to 4.6

And still there is same issue - WMF has been generated with 0 bytes length.

Can you check what happens if you try to create a PNG on you system

PNG was generated correctly.

So I suppose the main problem is what you mentioned:

Mono states that the Handle function in their libgdipus are not (fully) implemented [...]

mustelan avatar Oct 01 '19 12:10 mustelan