使用SharpDX绘制SVG图形

3

自Windows Creators Update以来,Direct2D支持SVG图像(文档):https://msdn.microsoft.com/en-us/library/windows/desktop/mt790715.aspx,通过ID2D1DeviceContext5(在SharpDX术语中为DeviceContext5)。 - Simon Mourier
@SimonMourier 感谢提供链接。我已经搜索了最近几天,但实际上没有任何关于这个的例子 :( - Jenny
你怎么看待这个样例?你有一个应用程序框架可以在其中查看它吗? - Simon Mourier
@SimonMourier 是的,我使用了它们在 GitHub 仓库 (https://github.com/sharpdx/SharpDX-Samples) 中的示例框架。 - Jenny
哪一个准确地说? - Simon Mourier
@SimonMourier 我正在使用这个进行测试:https://github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/Direct2D1/AdvancedTextRenderingApp (Direct2D和漂亮的模板),并从NuGet更新了SharpDX版本。 - Jenny
1个回答

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


关于注释1):由于我提交的Pull Request以支持此功能是在最新的“官方”发布之后,因此您需要一个预发布包。 - mrvux
此外,您正在每帧从文件创建和加载svg文档,只有ctx.DrawSvgDocument(doc);需要在主循环中,加载应在其外部完成。 - mrvux
@catflier 你说得对,这只是一个示例。 - Simon Mourier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接