title author date CreateTime categories
本文说如何显示SVG
lindexi
2019-09-02 12:57:38 +0800
2018-2-13 17:23:3 +0800

【】
本来在我一个白天晚上按钮,使用图片,图片不清晰

这些图片在http://www.zcool.com.cn/,不知道是不是不能直接用
我们需要一个看起来不会模糊,因为矢量图,所以我们就使用svg,其实png也是,但是他播放模糊。

lindexi

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve">
<g>
<circle fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" cx="32" cy="32" r="16"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="32" y1="10" x2="32" y2="0"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="32" y1="64" x2="32" y2="54"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="54" y1="32" x2="64" y2="32"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="0" y1="32" x2="10" y2="32"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="48" y1="16" x2="53" y2="11"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="11" y1="53" x2="16" y2="48"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="48" y1="48" x2="53" y2="53"/>
<line fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" x1="11" y1="11" x2="16" y2="16"/>
</g>
</svg>

我们开始使用Image,但是没有显示

于是网上有一个库Mntone.SvgForXaml,https://github.com/mntone/SvgForXaml,我们用Nuget

安装Mntone.SvgForXaml,安装win2d 1.11.0

我们上面那个代码就是svg,我们使用ViewModel绑定,绑定内容是SvgDocument

自然我们需要写一个字符串去转换

 private void Svgimage()
{
string str = @"<?xml version=""1.0"" encoding=""utf-8""?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC ""-//W3C//DTD SVG 1.1//EN"" ""http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"">
<svg version=""1.1"" id=""Layer_1"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" x=""0px"" y=""0px""
width=""64px"" height=""64px"" viewBox=""0 0 64 64"" enable-background=""new 0 0 64 64"" xml:space=""preserve"">
<g>
<circle fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" cx=""32"" cy=""32"" r=""16""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""32"" y1=""10"" x2=""32"" y2=""0""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""32"" y1=""64"" x2=""32"" y2=""54""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""54"" y1=""32"" x2=""64"" y2=""32""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""0"" y1=""32"" x2=""10"" y2=""32""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""48"" y1=""16"" x2=""53"" y2=""11""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""11"" y1=""53"" x2=""16"" y2=""48""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""48"" y1=""48"" x2=""53"" y2=""53""/>
<line fill=""none"" stroke=""#000000"" stroke-width=""2"" stroke-miterlimit=""10"" x1=""11"" y1=""11"" x2=""16"" y2=""16""/>
</g>
</svg>
";
Svg=SvgDocument.Parse(str); }

然后我们在我们的界面

先使用命名Mntone.SvgForXaml.UI.Xaml

xmlns:svg="using:Mntone.SvgForXaml.UI.Xaml"

然后绑定

 <Grid>
<svg:SvgImage x:Name="Svg" Content="{x:Bind View.Svg,Mode=OneWay}"></svg:SvgImage>
</Grid>

当然我们也可以放在我们的解决方案,假如我们的图片 Assets/weather_sun.svg

那么我们可以给我们的svgImage一个x:Name="Svg"

file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/weather_sun.svg"));

await Svg.LoadFileAsync(file);

原文:因为他会占用内存,我们需要手动把它释放

我们写在我们页面关掉,其实这个并不是关掉,只是我们的页面不显示

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Svg.SafeUnload();
}

我们可以简单把svg转换为我们之前的图片,JPG,png

先让用户选择保存的文件,然后选择.jpg或.png

var picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.DefaultFileExtension = ".png";
picker.FileTypeChoices.Add(new KeyValuePair<string, IList<string>>("Bitmap image", new[] { ".bmp" }.ToList()));
picker.FileTypeChoices.Add(new KeyValuePair<string, IList<string>>("Png image", new[] { ".png" }.ToList()));
picker.FileTypeChoices.Add(new KeyValuePair<string, IList<string>>("Jpeg image", new[] { ".jpg", ".jpe", ".jpeg" }.ToList()));
picker.FileTypeChoices.Add(new KeyValuePair<string, IList<string>>("Gif image", new[] { ".gif" }.ToList()));

SvgImageRendererFileFormat format;可以SvgImageRendererFileFormat.Bitmap或者什么自己选

await SvgImageRenderer.RendererImageAsync(file, new SvgImageRendererSettings()
{
Document = content,
Format = format,
Scaling = 0.1f,
Quality = 0.95f
});

参见:http://igrali.com/2015/12/24/how-to-render-svg-in-xaml-windows-10-uwp/

代码:https://github.com/lindexi/UWP/tree/master/uwp/src/ScalableVectorGraphic

因为已经有人写了,和我一样,驸马说他用过Svg to Xaml 做,我求请教了驸马大神,在github找到库,好像简单。

最新文章

  1. rc.local文件
  2. csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell
  3. Android——五大布局
  4. 常用sql,在做项目时用mysqlWorkBeach里面自动生成的
  5. 。。。Spring框架总结(一)。。。
  6. AIX 中 Paging Space 使用率过高的分析与解决
  7. Java NIO之Selector
  8. 10.29 morning
  9. The Standard Librarian: I/O and Function Objects: Containers of Pointers
  10. 使用JAVASCRIPT实现静态物体、静态方法和静态属性
  11. 正则表达式大全 --【Python举例】
  12. Xcode修改包名(含cocopods)
  13. Java面试题解构
  14. 【前端】Vue和Vux开发WebApp日志一、整合vue+cordova和webpack+gulp
  15. 关于Goldwell平台推出赠金及手数奖励
  16. MySQL查看SQL语句执行效率
  17. 一小时上手Java 8新特性
  18. CentOS 7 用firewall-cmd来开放端口
  19. display为inline-block的元素有内容和没有内容情况下高度不一致的问题
  20. ERROR 1666 (HY000): Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.

热门文章

  1. Django框架Day3------之Models
  2. 为Array对象添加一个去除重复项的方法
  3. pl/sql 语句块循环语句
  4. CSS user-select文本是否可复制
  5. java 使用poi导出Excel,设置单元格保护不可编辑
  6. 洛谷 P1505 [国家集训队]旅游 树链剖分
  7. Linux中的库
  8. vagrant 安装 ubuntu
  9. python 并发之进程
  10. Python学习之路5☞文件处理