代码之家  ›  专栏  ›  技术社区  ›  Jenny

使用SharpDX绘制SVG图形

  •  3
  • Jenny  · 技术社区  · 7 年前

    我正在C#中为Direct2D使用SharpDX。

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

    如何呈现SVG图形?

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Simon Mourier    7 年前

    如果您从 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 .