SVG Image loaded with ImageSource.FromStream() not displayed
See original GitHub issueDescription
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
- Load a SVG File (PNG works fine)
FileResult fileData = await MediaPicker.PickPhotoAsync(options);
- Read data from the file
var imageData = await fileData.OpenReadAsync();
- Get byte[] from imageData
byte[] imageByteArray = new byte[imageData.Length];
await imageData.ReadAsync(imageByteArray, 0, (int)imageData.Length);
ImageData = imageByteArray;
- 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));
- 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:
- Created 10 months ago
- Reactions:4
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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:
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?