渲染 SVG 图形的控件?

发布于 2024-11-09 07:07:29 字数 3871 浏览 0 评论 0原文

我需要一个用于渲染 SVG 图形的控件。数据以 StreamReader 对象的形式出现。渲染这样的图像最简单的方法是什么?

目前,我使用的是 PNG:

但我想要更高分辨率的。 SVG 非常适合此目的。


SVG 示例:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.28.0 (20110507.0327)
 -->
<!-- Title: G Pages: 1 -->
<svg width="262pt" height="216pt"
 viewBox="0.00 0.00 262.00 216.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph1" class="graph" transform="scale(1 1) rotate(0) translate(4 212)">
<title>G</title>
<polygon fill="white" stroke="white" points="-4,5 -4,-212 259,-212 259,5 -4,5"/>
<!-- a -->
<g id="node1" class="node"><title>a</title>
<polygon fill="purple" stroke="purple" points="159.618,-186.523 133,-198.872 106.382,-186.523 116.549,-166.541 149.451,-166.541 159.618,-186.523"/>
<polygon fill="none" stroke="purple" points="165.003,-188.397 133,-203.245 100.997,-188.397 114.139,-162.57 151.861,-162.57 165.003,-188.397"/>
<polygon fill="none" stroke="purple" points="170.387,-190.272 133,-207.617 95.6126,-190.272 111.729,-158.598 154.271,-158.598 170.387,-190.272"/>
<text text-anchor="middle" x="133" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">a</text>
</g>
<!-- b -->
<g id="node3" class="node"><title>b</title>
<ellipse fill="none" stroke="black" cx="133" cy="-100" rx="27" ry="18"/>
<text text-anchor="middle" x="133" y="-96.3" font-family="Times New Roman,serif" font-size="14.00">b</text>
</g>
<!-- a&#45;&gt;b -->
<g id="edge2" class="edge"><title>a&#45;&gt;b</title>
<path fill="none" stroke="black" d="M133,-158.413C133,-149.086 133,-138.069 133,-128.192"/>
<polygon fill="black" stroke="black" points="136.5,-128.057 133,-118.057 129.5,-128.057 136.5,-128.057"/>
</g>
<!-- c -->
<g id="node4" class="node"><title>c</title>
<polygon fill="none" stroke="black" points="144.42,-41 22.2639,-41 -0.419833,-5 121.736,-5 144.42,-41"/>
<text text-anchor="middle" x="72" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">hello world</text>
</g>
<!-- b&#45;&gt;c -->
<g id="edge3" class="edge"><title>b&#45;&gt;c</title>
<path fill="none" stroke="black" d="M120.656,-83.8226C112.588,-73.903 101.855,-60.7069 92.5226,-49.2327"/>
<polygon fill="black" stroke="black" points="95.0581,-46.8031 86.0329,-41.2536 89.6275,-51.22 95.0581,-46.8031"/>
</g>
<!-- d -->
<g id="node6" class="node"><title>d</title>
<polygon fill="none" stroke="black" points="194,-3.55271e-015 225.296,-34.5 162.704,-34.5 194,-3.55271e-015"/>
<text text-anchor="middle" x="194" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">d</text>
</g>
<!-- b&#45;&gt;d -->
<g id="edge5" class="edge"><title>b&#45;&gt;d</title>
<path fill="none" stroke="black" d="M145.344,-83.8226C154.961,-71.9983 168.365,-55.5183 178.67,-42.8489"/>
<polygon fill="black" stroke="black" points="181.629,-44.757 185.224,-34.7906 176.199,-40.3401 181.629,-44.757"/>
</g>
<!-- e -->
<g id="node7" class="node"><title>e</title>
<polygon fill="none" stroke="black" points="254.137,-199 189.863,-199 208.407,-163 235.593,-163 254.137,-199"/>
<text text-anchor="middle" x="222" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">e</text>
</g>
</g>
</svg>

I need a control for rendering SVG graphics. The data is coming in the in the form of a StreamReader object. What's the easiest way to render such an image?

At present, I'm using PNGs:

But I'd like something higher resolution. SVGs would be perfect for this.


Sample SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.28.0 (20110507.0327)
 -->
<!-- Title: G Pages: 1 -->
<svg width="262pt" height="216pt"
 viewBox="0.00 0.00 262.00 216.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph1" class="graph" transform="scale(1 1) rotate(0) translate(4 212)">
<title>G</title>
<polygon fill="white" stroke="white" points="-4,5 -4,-212 259,-212 259,5 -4,5"/>
<!-- a -->
<g id="node1" class="node"><title>a</title>
<polygon fill="purple" stroke="purple" points="159.618,-186.523 133,-198.872 106.382,-186.523 116.549,-166.541 149.451,-166.541 159.618,-186.523"/>
<polygon fill="none" stroke="purple" points="165.003,-188.397 133,-203.245 100.997,-188.397 114.139,-162.57 151.861,-162.57 165.003,-188.397"/>
<polygon fill="none" stroke="purple" points="170.387,-190.272 133,-207.617 95.6126,-190.272 111.729,-158.598 154.271,-158.598 170.387,-190.272"/>
<text text-anchor="middle" x="133" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">a</text>
</g>
<!-- b -->
<g id="node3" class="node"><title>b</title>
<ellipse fill="none" stroke="black" cx="133" cy="-100" rx="27" ry="18"/>
<text text-anchor="middle" x="133" y="-96.3" font-family="Times New Roman,serif" font-size="14.00">b</text>
</g>
<!-- a->b -->
<g id="edge2" class="edge"><title>a->b</title>
<path fill="none" stroke="black" d="M133,-158.413C133,-149.086 133,-138.069 133,-128.192"/>
<polygon fill="black" stroke="black" points="136.5,-128.057 133,-118.057 129.5,-128.057 136.5,-128.057"/>
</g>
<!-- c -->
<g id="node4" class="node"><title>c</title>
<polygon fill="none" stroke="black" points="144.42,-41 22.2639,-41 -0.419833,-5 121.736,-5 144.42,-41"/>
<text text-anchor="middle" x="72" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">hello world</text>
</g>
<!-- b->c -->
<g id="edge3" class="edge"><title>b->c</title>
<path fill="none" stroke="black" d="M120.656,-83.8226C112.588,-73.903 101.855,-60.7069 92.5226,-49.2327"/>
<polygon fill="black" stroke="black" points="95.0581,-46.8031 86.0329,-41.2536 89.6275,-51.22 95.0581,-46.8031"/>
</g>
<!-- d -->
<g id="node6" class="node"><title>d</title>
<polygon fill="none" stroke="black" points="194,-3.55271e-015 225.296,-34.5 162.704,-34.5 194,-3.55271e-015"/>
<text text-anchor="middle" x="194" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">d</text>
</g>
<!-- b->d -->
<g id="edge5" class="edge"><title>b->d</title>
<path fill="none" stroke="black" d="M145.344,-83.8226C154.961,-71.9983 168.365,-55.5183 178.67,-42.8489"/>
<polygon fill="black" stroke="black" points="181.629,-44.757 185.224,-34.7906 176.199,-40.3401 181.629,-44.757"/>
</g>
<!-- e -->
<g id="node7" class="node"><title>e</title>
<polygon fill="none" stroke="black" points="254.137,-199 189.863,-199 208.407,-163 235.593,-163 254.137,-199"/>
<text text-anchor="middle" x="222" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">e</text>
</g>
</g>
</svg>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

郁金香雨 2024-11-16 07:07:29

当我研究在 WPF 应用程序中使用 SVG 时,我发现可以添加几个包来提供此功能,但最终还是使用了我转换为 XAML 的 SVG,这将能够扩展WPF 应用程序就像 SVG 图像能够在浏览器等上缩放一样。如果您可以访问 SVG 代码(看起来您确实可以访问),那么这对您来说可能是一个很好的解决方案。

以下是我实现此目的的步骤:

将 SVG 转换为 XAML
我更喜欢使用 Inkscape 执行以下步骤。

  1. 在图像编辑器(例如 Inkscape)中打开 SVG。编辑器必须支持打开 SVG 并将 SVG 保存为 XAML 文件。
  2. 在编辑器中,将文件另存为 XAML。如果可以选择,该文件应保存为 Silverlight 兼容文件。关闭图像编辑器。
  3. 如果需要任何颜色更改,请在文本编辑器(例如 Notepad++、Visual Studio)中打开文件并查找具有颜色值的任何路径(查找类似“fill=”#000000””的内容)并将其更改为所需的十六进制颜色值。完成后保存文件并关闭文本编辑器。

在 WPF 项目中使用 XAML 图像

  1. 右键单击​​应保存图像的资源文件夹(例如 \Resources\Images),选择添加现有文件的选项。
    确保文件选择类型包括 XAML 文件。导航到要使用的文件并添加它。
  2. 打开 Visual Studio 中的“属性”窗格(右键单击该文件并单击“属性”选项)。在“属性”窗格的“高级”部分下,将“构建操作”设置为“资源”,并将“复制到输出目录”设置为“不复制”。

现在可以通过将项目的源设置为将图像(例如框架)显示到资源路径(例如“/resources/images/.xaml”)来使用图像。这可以直接设置或通过绑定设置。

用于显示 XAML 图像的示例 WPF/XAML 代码
以下代码显示了如何显示使用上述步骤添加的 XAML 图像的示例。此代码将缩放图像以填充其添加到的容器。图像的源是通过用于 Frame 元素源的绑定提供的。这可以替换为图像路径的字符串(这是通过绑定值提供的)。

<Viewbox Stretch="Uniform"
         Margin="4,4"
         VerticalAlignment="Center">
             <Frame Source="{Binding ImageSource}" 
                    NavigationUIVisibility="Hidden"/>
</Viewbox>

路径字符串的示例如下:

/resources/images/<file-name>.xaml

如果图像位于 DLL 中并在同一 DLL 中使用,则路径字符串将需要使用以下格式包含更多信息:

/<AssemblyName>;component/resources/images/<file-name>.xaml

When I had looked into using SVGs in my WPF applications, I found there were a couple packages that could be added in to provide this functionality, but in the end went with using SVGs that I converted to XAML, which will be able to scale in WPF applications the same way an SVG image is able to scale on browsers and such. If you have access to the SVG code (which it looks like you do), then this may be a good solution for you.

These are the steps I use to achieve this:

Converting SVG to XAML
I prefer to use Inkscape for the following steps.

  1. Open the SVG in a image editor (e.g. Inkscape). The editor must support opening an SVG and saving the SVG as a XAML file.
  2. From the editor, save the file as a XAML. The file should be saved as Silverlight compatible if given the choice. Close the image editor.
  3. If any color changes are desired, open the file in a text editor (e.g. Notepad++, Visual Studio) and find any paths with a color value (look for something like 'fill=“#000000”') and change it to the desired hex color value. Save the file and close the text editor when finished.

Using a XAML image in a WPF project

  1. Right click the resources folder the image should be kept in (e.g. \Resources\Images), pick the option to add an exisitng file.
    Make sure the file selection type includes XAML files. Navigate to the file to use and add it.
  2. Open the Properties pane in Visual Studio (right click the file and click the 'Properties' option). Under the 'Advanced' section in the Properties pane, set 'Build Action' to 'Resource' and Copy to Output Directory' to 'Do not copy'.

The image can now be used by setting the source of an item to display the image (e.g. a Frame) to the resource path (e.g. “/resources/images/.xaml”). This can be set either directly or through binding.

Example WPF/XAML code for displaying a XAML image
The following code shows an example of how to display a XAML image added using the above steps. This code will scale the image to fill the container it is added to. The source for the image is supplied through the binding used for the Frame element's source. This can be replaced with a string of the image's path (which is what is be provided through the binding value).

<Viewbox Stretch="Uniform"
         Margin="4,4"
         VerticalAlignment="Center">
             <Frame Source="{Binding ImageSource}" 
                    NavigationUIVisibility="Hidden"/>
</Viewbox>

An example of the path string is:

/resources/images/<file-name>.xaml

If the image is located in a DLL and is used in the same DLL, the path string will need to include more information using the following format:

/<AssemblyName>;component/resources/images/<file-name>.xaml
卖梦商人 2024-11-16 07:07:29

您还可以考虑使用 Web 渲染控件,例如 WebView2,显示 SVG 文件。

另一个想法是将 SVG 文件渲染为图像,即。使用 Svg.Skia,并显示渲染的图像。

You can also consider a web render control, like WebView2, to display the SVG file.

Another idea is to render the SVG file to an image, ie. using Svg.Skia, and display the rendered image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文