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
复制代码

 

posted @   唐宋元明清2188  阅读(2843)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示
哥伦布
01:11发布
哥伦布
01:11发布
8°
西北风
2级
空气质量
相对湿度
81%
今天
中雨
3°/15°
周四
大雨
2°/14°
周五
小雨
4°/10°