使用SharpDX绘制SVG图形

时间:2018-02-08 16:17:14

标签: c# sharpdx

我在C#中使用SharpDX用于Direct2D。

对于SVG,我发现SharpDX支持它:https://github.com/sharpdx/SharpDX/pull/954/files#diff-ab708c02d6344a22d99b1c932b72cc12

如何渲染SVG图形?

例如,我想绘制这条鲸鱼:https://www.flaticon.com/free-icon/whale_123995

1 个答案:

答案 0 :(得分:2)

如果从AdvancedTextRenderingApp示例开始,只需修改渲染循环,使其如下所示:

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();
    }
});

结果如下:

enter image description here

注1:您将需要SharpDX.Direct2D1预发布包(我的版本是4.1.0-ci217),否则关键CreateSvcDocument将因某些原因无法使用。

注2:ID2D1DeviceContext5(SharpDX中的DeviceContext5)需要Windows 10 Creators Update