WPF Geometry 添加Path数据

时间:2021-12-02 08:50:49

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

DrawingImage:

     <DrawingImage x:Key="Image.Search">
<DrawingImage.Drawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<SolidColorBrush Color="#FF666666" Opacity="0.5" />
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero"
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" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>

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

     <StreamGeometry x:Key="Path.Stream.Search">
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
</StreamGeometry>

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

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

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

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

     <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>
<PathGeometry x:Key="Path.Search" Figures="{StaticResource PathData}"/>
<DrawingImage x:Key="Image.Search">
<DrawingImage.Drawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<SolidColorBrush Color="#FF666666" Opacity="0.5" />
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero"
Figures="{StaticResource PathData}" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>

显示效果:

 <Window x:Class="WpfApp8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp8"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<StreamGeometry x:Key="Path.Stream.Search">
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
</StreamGeometry> <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>
<PathGeometry x:Key="Path.Search" Figures="{StaticResource PathData}"/>
<DrawingImage x:Key="Image.Search">
<DrawingImage.Drawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<SolidColorBrush Color="#FF666666" Opacity="0.5" />
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero"
Figures="{StaticResource PathData}" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Window.Resources>
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Path Fill="Red" Data="{StaticResource Path.Stream.Search}"/>
<Path Fill="Red" Data="{StaticResource Path.Search}"/>
<Image Source="{StaticResource Image.Search}" Margin="0 10 0 0" Stretch="None"></Image>
</StackPanel>
</Grid>
</Window>

WPF Geometry 添加Path数据