【问题标题】:Rendering text marker in SvgImageSource在 SvgImageSource 中渲染文本标记
【发布时间】:2020-09-12 00:52:15
【问题描述】:

我现在正在使用 UWP 应用程序,我需要从网址转换 SVG 图像。我用新的 Uri 创建了新的 SvgImageSource,但图像渲染不正确。在 SVG 文档中,我有 2 个标记:矩形和文本。矩形正确呈现,但文本无法呈现。有谁知道如何解决这个问题?
C#代码:

public async Task<ImageSource> GetAvatar(string address)
        {
            using(var client = new HttpClient())
            {
                var response = await client.GetAsync(address);
                string content = await response.Content.ReadAsStringAsync();
                if(content.Substring(0, 4).Equals("<svg"))
                {
                    var svg = new SvgImageSource(new Uri(address));
                    return svg;
                }
                return new BitmapImage();
            }
        }

SVG:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect width="100%" height="100%" fill="#FF5722"/>
<text x="50%" y="50%" dy="0.36em" text-anchor="middle" pointer-events="none" fill="#ffffff" font-family="'Helvetica', 'Arial', 'Lucida Grande', 'sans-serif'" font-size="125">
M
</text>
</svg>

【问题讨论】:

  • 您从哪里获得示例 SVG?字体类型和大小可能不在您的机器上。 125 字号非常大。
  • 内容来自 Rocket.Chat API。当您注册新帐户时,脚本会在 SVG 中为您生成头像

标签: c# .net xaml svg uwp


【解决方案1】:

恐怕 UWP 平台不支持 text svg 元素。请检查SVG Support 文本元素不在支持列表中。目前有一种解决方法是将文本转换为path 元素,它会起作用。

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect width="100%" height="100%" fill="#FF5722"/>
<path  d="M 81.055 101.5 L 81.055 17.545 L 53.94 78.59 L 40.31 78.59 L 13.05 17.545 L 13.05 101.5 L 0 101.5 L 0 0 L 19.285 0 L 47.415 64.235 L 75.11 0 L 94.83 0 L 94.83 101.5 L 81.055 101.5 Z" vector-effect="non-scaling-stroke"/>
</svg>

【讨论】:

  • ouch :( 感谢您的回复。我从服务器地址获取 SvgDoc,因此无法解决此问题。
【解决方案2】:

看下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string URL = "https://raw.githubusercontent.com/RocketChat/Rocket.Chat.Artwork/master/Logos/logo-dark.svg";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(URL);
            string text = doc.ToString();
            Console.WriteLine(text);
            Console.ReadLine();
        }
    }
}

【讨论】:

  • 从 .net core 3.1 我无权访问命名空间 System.Xml。在 UWP 中,我有 SvgImageSource,我可以在我的视图中绑定到 ImageSource。我昨天回答了有关 SVG 支持的问题,但感谢您的宝贵时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多