在 WPF 中使用矢量图像的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13292179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to use a vector image in WPF?
提问by
I am looking for a good method to add a vector file (EPS or SVG) in a XAML. I found a plugin that exports image from Illustrator to a XAML file, let's say MyImage.xaml, and it works well if I copy the content of the file in my XAML file for my window (Plugin link: http://www.mikeswanson.com/XAMLExport/).
我正在寻找一种在 XAML 中添加矢量文件(EPS 或 SVG)的好方法。我找到了一个插件,可以将图像从 Illustrator 导出到 XAML 文件,比如说 MyImage.xaml,如果我将文件内容复制到我的窗口的 XAML 文件中(插件链接:http://www.mikeswanson) .com/XAMLExport/)。
However I am sure better ways exist. Is it possible for example to use MyImage.xaml as a resource or something, and import it in the XAML that describes the window ?
但是我确信存在更好的方法。例如,是否可以将 MyImage.xaml 用作资源或其他东西,并将其导入到描述窗口的 XAML 中?
采纳答案by Chris W.
Personally, if you're talking about using it in multiple places without having to re-use / re-draw your xaml paths each time. Then I just plop them in a ContentControl like;
就个人而言,如果您要在多个地方使用它而不必每次都重新使用/重新绘制您的 xaml 路径。然后我只是将它们放入一个 ContentControl 中;
<!-- Plop this in your resource dictionary or your resource declaration -->
<Style x:Key="TheAwesomeXAMLimage" TargetType="ContentControl">
<!-- Add additional Setters Here -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<!-- Just Paste your XAML here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Now actually place it on your view -->
<ContentControl Style="{StaticResource TheAwesomeXAMLimage}"/>
回答by ΩmegaMan
Use a DrawingImage, as your containerwhich is designed to be such a wrapper:
使用DrawingImage作为您的容器,它被设计为这样的包装器:
<MyWindow.Resources>
<DrawingImage x:Key="diUndo">
<DrawingImage.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#FF22BAFD" Geometry="..."/>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</MyWindow.Resources>
Then reuse it as such:
然后像这样重用它:
<Image Source="{DynamicResource diUndo}" />
but it will always be that color.....
但它永远是那个颜色.....
Or Specify a Color
或指定颜色
Make the vector a style and then change up dynamically at the target (its a vector and not a static image right?), such as fill color as needed:
使矢量成为一种样式,然后在目标处动态更改(它是矢量而不是静态图像,对吗?),例如根据需要填充颜色:
<MyWindow.Resources>
<Path x:Key="vRedo"
Data="F1M14.4401,25.5039C15.5755,22.9375 17.1667,20.5703 19.2162,18.5239 23.5781,14.1587 29.3828,11.7539 35.5573,11.7539 41.7344,11.7539 47.5365,14.1587 51.8984,18.5239 56.263,22.8879 58.6667,28.6899 58.6667,34.8645 58.6667,41.0391 56.263,46.8411 51.8984,51.2056 51.2031,51.8997 50.2917,52.2461 49.3828,52.2461 48.474,52.2461 47.5599,51.8997 46.8646,51.2056 45.4818,49.8164 45.4818,47.5664 46.8698,46.177 49.8932,43.1563 51.5573,39.1392 51.5573,34.8645 51.5573,30.5911 49.8932,26.5728 46.8646,23.552 43.849,20.5273 39.8307,18.8645 35.5573,18.8645 31.2813,18.8645 27.2656,20.5273 24.2448,23.552 22.0052,25.7915 20.5182,28.5845 19.8932,31.6184L27.5573,40.1992 5.33334,40.1992 7.10938,17.2969 14.4401,25.5039z" />
<Style x:Key="ModifiablePathStyle"
TargetType="{x:Type Path}">
<Setter Property="Stretch"
Value="Uniform" />
<Setter Property="Data"
Value="{Binding Data, Source={StaticResource vRedo}}" />
</Style>
</MyWindow.Resources>
Here is its usage:
这是它的用法:
<Path Style="{StaticResource ModifiablePathStyle}" Fill="Blue"/>
<Path Style="{StaticResource ModifiablePathStyle}" Fill="Red"/>
<Path Style="{StaticResource ModifiablePathStyle}" Fill="Green"/>
Here is the result of all three:
这是所有三个的结果:


回答by Sisyphe
If you are not against the use of 3rd party tools, considere having a look at SharpVectors. It's doing a great job with SVG : Parsing, XAML conversion, displaying, etc.
如果您不反对使用 3rd 方工具,请考虑查看SharpVectors。它在 SVG 方面做得很好:解析、XAML 转换、显示等。
Edit : I may have not understood your question, you may be better with Chris W. answer ;)
编辑:我可能没有理解你的问题,你可能会更好地回答 Chris W. ;)

