Search code examples
uno-platform

Local Image not displayed in Skia.WPF Uno Platform project


I save images relative to the workingdirectory and then want to display them in a uno Skia.WPF app.

However they never appear. This is how I create the ImageSource:

public static async Task<ImageSource> GenerateSource()
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.FileTypeFilter.Add(".png");
    var pickerResult = await picker.PickSingleFileAsync("tileset");
    if (pickerResult is null)
        return null;
    using var stream = await pickerResult.OpenReadAsync();

    string imagePath = Path.GetFullPath(Path.Combine("cache", "image.png"));
    Directory.CreateDirectory(Path.GetDirectoryName(imagePath));

    using (var bitmapImageStream = File.Open(imagePath,
                         FileMode.Create,
                         FileAccess.Write,
                         FileShare.None))
        {

            await stream.AsStreamForRead().CopyToAsync(bitmapImageStream);
            bitmapImageStream.Flush(true);
        }

    var imgSrc = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    imgSrc.UriSource = new Uri($"file://{imagePath}");
    var width = imgSrc.PixelHeight;
    return imgSrc;
}

But when I use the path directly in the Xaml it also not working.

<Image Source="file://cache/image.png" Width="32" Height="32"  />

Using an image from the Internet using an http url works.

Sample Repo


Solution

  • file: URIs are currently not supported in Uno, however, you can still show local images by copying to the appropriate folder using ms-appdata scheme. If you copy the file into ApplicationData.Current.TemporaryFolder, you will the be able to reference that path by ms-appdata:///temp/{imageName} URI.