【问题标题】:Draw SVG graphic with SharpDX使用 SharpDX 绘制 SVG 图形
【发布时间】:2023-03-26 07:36:01
【问题描述】:

我在 C# 中为 Direct2D 使用 SharpDX。

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

如何渲染 SVG 图形?

比如我要画这条鲸鱼:https://www.flaticon.com/free-icon/whale_123995

【问题讨论】:

  • 自 Windows Creators Update 以来,Direct2D 支持 SVG 图像(文档):msdn.microsoft.com/en-us/library/windows/desktop/mt790715.aspx 到 ID2D1DeviceContext5(我相信 SharpDX 术语中的 DeviceContext5)
  • @SimonMourier 感谢您的链接。最近几天我正在搜索,实际上没有这方面的例子:(
  • 您如何看待样本?您是否有一个可以在其中看到它的应用程序骨架?
  • @SimonMourier 是的,我使用他们 github 存储库 (github.com/sharpdx/SharpDX-Samples) 中的示例骨架
  • 究竟是哪一个?

标签: c# sharpdx


【解决方案1】:

如果您从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();
    }
});

结果如下:

注意 1:您将需要 SharpDX.Direct2D1 预发布包(我的版本是 4.1.0-ci217),否则由于某种原因,关键的 CreateSvcDocument 将不可用。

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

【讨论】:

  • 关于注释 1) :您需要一个预发布包,因为我提交的支持此请求的拉取请求发生在最新的“官方”发布之后
  • 另外,您每帧都从文件中创建和加载 svg 文档,只有 ctx.DrawSvgDocument(doc);在主循环中是必需的,加载应该在它之外完成。
  • @catflier 你是对的,这只是一个示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多