question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG Image loaded with ImageSource.FromStream() not displayed

See original GitHub issue

Description

I’m loading PNG and SVG images stored in a database. I have gotten this to work correctly in the Xamarin version of the application, using FFImageLoading Nuget, using the SVGImageSource class.

PNG files work fine, but SVG are not displayed.

This is how it was on Xamarin:

//ImageFormat is a string that contains the value of FileResult.ContentType, to know if it is PNG or SVG

if (image.ImageFormat != null && image.ImageFormat.ToLower().Contains("svg"))
{
    imageSource = SvgImageSource.FromStream(() => new MemoryStream(image.ImageData));
}
else
{
    imageSource = ImageSource.FromStream(() => new MemoryStream(image.ImageData));
}

MAUI:

    imageSource = ImageSource.FromStream(() => new MemoryStream(image.ImageData));

The documentation inticates that ImageSource.FromStream can load SVG and also animated .GIF, so, no need to check if its PNG or SVG. Anyway, i haven’t found anything similar to SvgImageSource.

Steps to Reproduce

  1. Load a SVG File (PNG works fine)
FileResult fileData = await MediaPicker.PickPhotoAsync(options);
  1. Read data from the file
var imageData = await fileData.OpenReadAsync();
  1. Get byte[] from imageData
byte[] imageByteArray = new byte[imageData.Length];
await imageData.ReadAsync(imageByteArray, 0, (int)imageData.Length);
ImageData = imageByteArray;
  1. Get ImageSource from byte[] to bind it to an Image Control
//If you try to get an ImageSource from this byte[] the image is not displayed if is an SVG image
ImageSource ImageData = ImageSource.FromStream(() => new MemoryStream(item.ImageData));
  1. Bind the control on the view to the ImageData Property
<Image Source="{Binding ImageData }" WidthRequest="64" HeightRequest="64" Margin="4,0"/>

Link to public reproduction project repository

https://github.com/drossoft/SVGImageLoadSample

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

Windows SDK 10.0.19041.0, Android 11

Did you find any workaround?

Not yet

Relevant log output

Microsoft.Maui.StreamImageSourceService: Warning: Unable to load image stream.

System.Runtime.InteropServices.COMException (0x88982F50): The component cannot be found. (0x88982F50)
   at Microsoft.Maui.StreamImageSourceService.GetImageSourceAsync(IStreamImageSource imageSource, Single scale, CancellationToken cancellationToken)

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

3reactions
drossoftcommented, Apr 3, 2023

For anyone having trouble with this, I have found a solution. I have implemented a method using SkiaSharp to get an ImageSource from a byte[] (with the help of ChatGPT).

You need these nugets:

  • SkiaSharp.Svg (1.60.0)
  • SkiaSharp.Views.Maui.Core (2.88.3)
using SkiaSharp;

public ImageSource ConvertSvgToImageSource(byte[] svgData)
{
            using (var stream = new MemoryStream(svgData))
            {
                var svgDocument = new SkiaSharp.Extended.Svg.SKSvg();
                svgDocument.Load(stream);

                var bitmap = new SKBitmap((int)svgDocument.Picture.CullRect.Width, (int)svgDocument.Picture.CullRect.Height);

                using (var surface = SKSurface.Create(new SKImageInfo(bitmap.Width, bitmap.Height)))
                {
                    var canvas = surface.Canvas;
                    canvas.Clear(SKColors.Transparent);
                    canvas.DrawPicture(svgDocument.Picture);
                    surface.Canvas.Flush();

                    var image = surface.Snapshot();
                    bitmap = SKBitmap.FromImage(image);
                    var data = bitmap.Encode(SKEncodedImageFormat.Png, 100);

                    var imageStream = new MemoryStream(data.ToArray());
                    var imageSource = ImageSource.FromStream(() => imageStream);

                    return imageSource;
                }
            }
}
0reactions
drossoftcommented, Mar 11, 2023

I’ve tried caching the image in the file and loading it using ImageSource.FromFile, but it still doesn’t work for SVG files.

string localPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string imagesPath = Path.Combine(localPath, “Images”);

string imageFullPath = Path.Combine(imagesPath, “image_test.svg”);

File.WriteAllBytes(imageFullPath, imageBytes); //imageBytes is the byte[] read from database image = ImageSource.FromFile(imageFullPath);

I see that the image is correctly saved to the file when I open it in File Explorer, and the variable “image” is an ImageSource pointing to the file, but it doesn’t show up when I bind it in my view.

@daDomas, have you checked that SVG images work with your solution or only JPG images?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image not displaying in .NET MAUI Mac Catalyst
1 Answer. I had create a sample to test your code and the image can't show either. I found that you have changed...
Read more >
Image - .NET MAUI
The .NET MAUI Image displays an image that can be loaded from a local file, a URI, or a stream.
Read more >
How to load an svg image to the XRPictureBox control at ...
Use the SvgImage.FromStream method to create a new SvgImage object from a vector image stored in a stream. Please let me know if...
Read more >
SvgImageSource Class (Windows.UI.Xaml.Media.Imaging)
Occurs when the SVG source is downloaded and decoded with no failure. Occurs when there is an error associated with SVG retrieval or...
Read more >
<image> - SVG: Scalable Vector Graphics - MDN Web Docs
The <image> SVG element includes images inside SVG documents. It can display raster image files or other SVG files. The only image formats ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found