Avalonia常用小控件Svg

1.项目下载地址:https://gitee.com/confusedkitten/avalonia-demo

2.UI库Semi.Avalonia,项目地址  https://github.com/irihitech/Semi.Avalonia

3.SVG库,Avalonia.Svg.Skia,项目地址 https://github.com/wieslawsoltes/Svg.Skia

4.样式预览:

Avalonia常用小控件Svg_第1张图片

 5.Svg.axaml


	
		
			
			
		

		
			
			
		

		
			
			
		
	

  6.Svg.axaml.cs

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Svg.Skia;
using System.IO;
using System;
using Avalonia.Interactivity;
using Avalonia.Controls.Shapes;

namespace AvaloniaDemo.Pages;

public partial class Svg : UserControl
{
    public Svg()
    {
        InitializeComponent();
        Loaded += WindowLoaded;
    }

    private void WindowLoaded(object? sender, RoutedEventArgs e)
    {
        string baseurl = Directory.GetCurrentDirectory();
        var imagepath = System.IO.Path.Combine(baseurl, @"Assets\Images\new 4.svg");
        var ImageByte = File.ReadAllBytes(imagepath);
        var svgSource = ByteArrayToDrawingImage(ImageByte);
        ShowImage.Source = svgSource;
    }

    public SvgImage? ByteArrayToDrawingImage(Byte[] byteArray)
    {
        if (byteArray == null || byteArray?.Length == 0) return default;
        using (MemoryStream documentStream = new MemoryStream(byteArray))
        {
            try
            {
                var svg = new SvgSource();
                var picture = svg.Load(documentStream);
                if (picture is { })
                {
                    var svgImage = new Avalonia.Svg.Skia.SvgImage() { Source = svg };
                    return svgImage;
                }
            }
            catch (Exception ex)
            {

            }
        }
        return default;
    }
}

你可能感兴趣的:(Avalonia,Avalonia,Svg)