3

I am using SharpDX in C# for Direct2D.

For SVG I found that SharpDX supports it: https://github.com/sharpdx/SharpDX/pull/954/files#diff-ab708c02d6344a22d99b1c932b72cc12

How can I render SVG graphics?

For example I want to draw this whale: https://www.flaticon.com/free-icon/whale_123995

Jenny
  • 572
  • 4
  • 16
  • SVG images (documents) is supported by Direct2D since Windows Creators Update: https://msdn.microsoft.com/en-us/library/windows/desktop/mt790715.aspx through ID2D1DeviceContext5 (DeviceContext5 in SharpDX terms I believe) – Simon Mourier Feb 10 '18 at 09:19
  • @SimonMourier Thanks for the link. I am searching the last couple days and there are literally no examples on this :( – Jenny Feb 10 '18 at 11:02
  • How would you see the sample? Do you have an app skeleton in which you would see it? – Simon Mourier Feb 10 '18 at 13:28
  • @SimonMourier yes, I use the sample skeletons from their github repo (https://github.com/sharpdx/SharpDX-Samples) – Jenny Feb 10 '18 at 15:11
  • Which one precisely? – Simon Mourier Feb 10 '18 at 17:05
  • @SimonMourier I am testing with this one: https://github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/Direct2D1/AdvancedTextRenderingApp (Direct2D and nice template) and updated SharpDX version from NuGet. – Jenny Feb 10 '18 at 17:27

1 Answers1

2

If you start from the AdvancedTextRenderingApp sample, just modify the render loop so it looks like this:

RenderLoop.Run(mainForm, () =>
{
    renderTarget.BeginDraw();
    renderTarget.Clear(bgcolor);

    // get an ID2D1DeviceContext5 interface. It will fail here if Windows is not version 1704+
    using (var ctx = renderTarget.QueryInterface<DeviceContext5>())
    {
        // create WIC factory
        using (var imagingFactory = new ImagingFactory())
        {
            // load a file as a stream using WIC
            using (var file = File.OpenRead("whale.svg"))
            {
                using (var stream = new WICStream(imagingFactory, file))
                {
                    // create the SVG document
                    using (var doc = ctx.CreateSvgDocument(stream, new Size2F(mainForm.Width, mainForm.Height)))
                    {
                        // draw it on the device context
                        ctx.DrawSvgDocument(doc);
                    }
                }
            }
        }
    }

    try
    {
        renderTarget.EndDraw();
    }
    catch
    {
        CreateResources();
    }
});

And here is the result:

enter image description here

Note 1: you will need the SharpDX.Direct2D1 prerelease package (my version is 4.1.0-ci217), otherwise the crucial CreateSvcDocument will not be available for some reason.

Note 2: ID2D1DeviceContext5 (DeviceContext5 in SharpDX) requires Windows 10 Creators Update.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • About note 1) : You need a prerelease package since the Pull Request I submitted to support this happened after the latest "official" release – mrvux Feb 16 '18 at 16:38
  • Also, you are creating and loading the svg document from file every frame, only ctx.DrawSvgDocument(doc); is required within the mainloop, the loading should be done outside of it. – mrvux Feb 16 '18 at 16:40
  • @catflier you're right, it's just an illustrating sample. – Simon Mourier Feb 16 '18 at 19:01