For the Uno platform, I need to load an image from "content" resources. I'm using GetManifestResourceStream
, but it's returning null while running the UWP project (I haven't tried the other flavors).
To dig a bit further, I added GetManifestResourceNames
, but it is also returning an empty array.
Here's my code, which is inside a UserControl
(Source
is a property):
private void LoadBmpSrc()
{
if (Source == null)
return;
Assembly assembly = GetType().GetTypeInfo().Assembly;
string[] names = assembly.GetManifestResourceNames();
using (Stream stream = assembly.GetManifestResourceStream(Source))
{
bmpSrc = SKBitmap.Decode(stream);
}
}
In the consuming XAML file, I have this, inside a Grid
:
<controls:ExpandableImage
Grid.Column="0"
Source="ms-appx:///Assets/icons/folder_tab.png"
/>
If I swap in this code:
<Image
Grid.Column="0"
Source="ms-appx:///Assets/icons/folder_tab.png"
/>
the image displays.
EDIT
Here's the XAML for the control:
<UserControl
x:Class="UnoTest.Shared.Controls.ExpandableImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UnoTest.Shared.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:skia="using:SkiaSharp.Views.UWP"
>
<skia:SKXamlCanvas x:Name="EICanvas" PaintSurface="OnPaintSurface" />
</UserControl>
Your problem is basically that the Content
images are not treated as Resources
.
If you want to include images as a Resource you will need to change the Build Action of that file into Embedded Resource. Once you change this it will show up in the
string[] names = assembly.GetManifestResourceNames();
method call.
You will notice that the name of the image will be something like ApplicationName.Directory.ImageName.png
. You will need to specify this name in order to load the image with the
assembly.GetManifestResourceStream(imageName)
If you want to load a Content Image
data and into a Stream you could do that (haven't test it) with the following code:
var imageUrl = "ms-appx:///Assets/SplashScreen.png";
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(imageUrl));
using(var stream = await file.OpenStreamForReadAsync())
{
//Use your data here
}
Hope this helps.-