Search code examples
c#xamlwinui-3

How to convert SVG image source to any IconSource in WinUI 3


I want to convert a SVG image to any kind of IconSource so I can use it in TabViewItem.IconSource

BitmapIconSource icon = new()
{
    UriSource = new Uri("http://url/image.svg"),//This doesn't work, BitmapIconSource cannot render svg
    ShowAsMonochrome = false,
};
TabViewItem.IconSource = icon;

SVGImageSource is not useable in IconSource. Am I able to convert SVG to IconSource or do I have to set it directly to the TabViewItem.Header like this?

SvgImageSource ImageSource = new() { UriSource = new Uri("http://url/image.svg") };
ImageIcon SVGIcon = new() { Source= ImageSource };
SVGIcon.Width = 15;
SVGIcon.Height = 15;
TabViewItem.Header = SVGIcon;

Solution

  • You would use an ImageIconSource (instead of a BitmapIconSource) with an SvgImageSource like this:

    using Microsoft.UI.Xaml.Controls;
    using Microsoft.UI.Xaml.Media.Imaging;
    ...
    
    var icon = new ImageIconSource
    {
        ImageSource = new SvgImageSource(new Uri("http://url/image.svg"))
    };
    
    tabViewItem.IconSource = icon;