WPF Geometry 添加Path数据

浪子不回头ぞ 提交于 2020-05-02 02:18:03
原文: WPF Geometry 添加Path数据

当图片转svg,svg转Xaml后,根据数据加载显示图片

DrawingImage:

 1     <DrawingImage x:Key="Image.Search">
 2         <DrawingImage.Drawing>
 3             <GeometryDrawing>
 4                 <GeometryDrawing.Brush>
 5                     <SolidColorBrush Color="#FF666666" Opacity="0.5" />
 6                 </GeometryDrawing.Brush>
 7                 <GeometryDrawing.Geometry>
 8                     <PathGeometry FillRule="Nonzero"
 9                               Figures="M6.5,2C4.01471853256226,2 2,4.01471853256226 1.99999988079071,6.5 2,8.98528099060059 4.01471853256226,11 6.5,11 8.98528099060059,11 11,8.98528099060059 11,6.5 11,4.01471853256226 8.98528099060059,2 6.5,2z M6.5,1.00000011920929C9.53756618499756,1 12,3.46243381500244 12,6.5 12,7.65749835968018 11.6424360275269,8.73148345947266 11.03173828125,9.61752414703369L13.9562683105469,12.5420551300049C14.3431911468506,12.9289779663086 14.3429946899414,13.5565004348755 13.9497470855713,13.9497470855713 13.5592231750488,14.3402719497681 12.923752784729,14.3379669189453 12.5420551300049,13.9562683105469L9.61752414703369,11.03173828125C8.73148345947266,11.6424360275269 7.65749835968018,12 6.5,12 3.46243381500244,12 1,9.53756618499756 0.999999940395355,6.5 1,3.46243381500244 3.46243381500244,1 6.5,1.00000011920929z" />
10                 </GeometryDrawing.Geometry>
11             </GeometryDrawing>
12         </DrawingImage.Drawing>
13     </DrawingImage>

也有可能,同时也需要Geometry:

1     <StreamGeometry x:Key="Path.Stream.Search">
2         M6.5,2C4.01471853256226,2 2,4.01471853256226 1.99999988079071,6.5 2,8.98528099060059 4.01471853256226,11 6.5,11 8.98528099060059,11 11,8.98528099060059 11,6.5 11,4.01471853256226 8.98528099060059,2 6.5,2z M6.5,1.00000011920929C9.53756618499756,1 12,3.46243381500244 12,6.5 12,7.65749835968018 11.6424360275269,8.73148345947266 11.03173828125,9.61752414703369L13.9562683105469,12.5420551300049C14.3431911468506,12.9289779663086 14.3429946899414,13.5565004348755 13.9497470855713,13.9497470855713 13.5592231750488,14.3402719497681 12.923752784729,14.3379669189453 12.5420551300049,13.9562683105469L9.61752414703369,11.03173828125C8.73148345947266,11.6424360275269 7.65749835968018,12 6.5,12 3.46243381500244,12 1,9.53756618499756 0.999999940395355,6.5 1,3.46243381500244 3.46243381500244,1 6.5,1.00000011920929z
3     </StreamGeometry>

如何将俩种引用方式,提取Path数据重用?

StreamGeometry不行, 没有相应的属性可以支持。。。。

所以我们换个Geomery,虽然没有StreamGeometry性能好。。。

 同时,使用PathFigureCollection装载Path数据,然后PathGeometry、DrawingImage 引用资源。

 1     <PathFigureCollection x:Key="PathData">M6.5,2C4.01471853256226,2 2,4.01471853256226 1.99999988079071,6.5 2,8.98528099060059 4.01471853256226,11 6.5,11 8.98528099060059,11 11,8.98528099060059 11,6.5 11,4.01471853256226 8.98528099060059,2 6.5,2z M6.5,1.00000011920929C9.53756618499756,1 12,3.46243381500244 12,6.5 12,7.65749835968018 11.6424360275269,8.73148345947266 11.03173828125,9.61752414703369L13.9562683105469,12.5420551300049C14.3431911468506,12.9289779663086 14.3429946899414,13.5565004348755 13.9497470855713,13.9497470855713 13.5592231750488,14.3402719497681 12.923752784729,14.3379669189453 12.5420551300049,13.9562683105469L9.61752414703369,11.03173828125C8.73148345947266,11.6424360275269 7.65749835968018,12 6.5,12 3.46243381500244,12 1,9.53756618499756 0.999999940395355,6.5 1,3.46243381500244 3.46243381500244,1 6.5,1.00000011920929z</PathFigureCollection>
 2     <PathGeometry x:Key="Path.Search" Figures="{StaticResource PathData}"/>
 3     <DrawingImage x:Key="Image.Search">
 4         <DrawingImage.Drawing>
 5             <GeometryDrawing>
 6                 <GeometryDrawing.Brush>
 7                     <SolidColorBrush Color="#FF666666" Opacity="0.5" />
 8                 </GeometryDrawing.Brush>
 9                 <GeometryDrawing.Geometry>
10                     <PathGeometry FillRule="Nonzero"
11                               Figures="{StaticResource PathData}" />
12                 </GeometryDrawing.Geometry>
13             </GeometryDrawing>
14         </DrawingImage.Drawing>
15     </DrawingImage>

 

 

显示效果:

 1 <Window x:Class="WpfApp8.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp8"
 7         xmlns:sys="clr-namespace:System;assembly=mscorlib"
 8         mc:Ignorable="d"
 9         Title="MainWindow" Height="450" Width="800">
10     <Window.Resources>
11         <StreamGeometry x:Key="Path.Stream.Search">
12             M6.5,2C4.01471853256226,2 2,4.01471853256226 1.99999988079071,6.5 2,8.98528099060059 4.01471853256226,11 6.5,11 8.98528099060059,11 11,8.98528099060059 11,6.5 11,4.01471853256226 8.98528099060059,2 6.5,2z M6.5,1.00000011920929C9.53756618499756,1 12,3.46243381500244 12,6.5 12,7.65749835968018 11.6424360275269,8.73148345947266 11.03173828125,9.61752414703369L13.9562683105469,12.5420551300049C14.3431911468506,12.9289779663086 14.3429946899414,13.5565004348755 13.9497470855713,13.9497470855713 13.5592231750488,14.3402719497681 12.923752784729,14.3379669189453 12.5420551300049,13.9562683105469L9.61752414703369,11.03173828125C8.73148345947266,11.6424360275269 7.65749835968018,12 6.5,12 3.46243381500244,12 1,9.53756618499756 0.999999940395355,6.5 1,3.46243381500244 3.46243381500244,1 6.5,1.00000011920929z
13         </StreamGeometry>
14 
15     <PathFigureCollection x:Key="PathData">M6.5,2C4.01471853256226,2 2,4.01471853256226 1.99999988079071,6.5 2,8.98528099060059 4.01471853256226,11 6.5,11 8.98528099060059,11 11,8.98528099060059 11,6.5 11,4.01471853256226 8.98528099060059,2 6.5,2z M6.5,1.00000011920929C9.53756618499756,1 12,3.46243381500244 12,6.5 12,7.65749835968018 11.6424360275269,8.73148345947266 11.03173828125,9.61752414703369L13.9562683105469,12.5420551300049C14.3431911468506,12.9289779663086 14.3429946899414,13.5565004348755 13.9497470855713,13.9497470855713 13.5592231750488,14.3402719497681 12.923752784729,14.3379669189453 12.5420551300049,13.9562683105469L9.61752414703369,11.03173828125C8.73148345947266,11.6424360275269 7.65749835968018,12 6.5,12 3.46243381500244,12 1,9.53756618499756 0.999999940395355,6.5 1,3.46243381500244 3.46243381500244,1 6.5,1.00000011920929z</PathFigureCollection>
16     <PathGeometry x:Key="Path.Search" Figures="{StaticResource PathData}"/>
17     <DrawingImage x:Key="Image.Search">
18         <DrawingImage.Drawing>
19             <GeometryDrawing>
20                 <GeometryDrawing.Brush>
21                     <SolidColorBrush Color="#FF666666" Opacity="0.5" />
22                 </GeometryDrawing.Brush>
23                 <GeometryDrawing.Geometry>
24                     <PathGeometry FillRule="Nonzero"
25                               Figures="{StaticResource PathData}" />
26                 </GeometryDrawing.Geometry>
27             </GeometryDrawing>
28         </DrawingImage.Drawing>
29     </DrawingImage>
30     </Window.Resources>
31     <Grid>
32         <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
33             <Path Fill="Red" Data="{StaticResource Path.Stream.Search}"/>
34             <Path Fill="Red" Data="{StaticResource Path.Search}"/>
35             <Image Source="{StaticResource Image.Search}" Margin="0 10 0 0" Stretch="None"></Image>
36         </StackPanel>
37     </Grid>
38 </Window>
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!