Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to display an SKBitmap image in Xamarin Forms (Android) using SkiaSharp. The PNG file is located in a folder named "Media" under the project "GraphicsTest.Android", and the Build Action property is set to "Embedded Resource". My code to create the bitmap follows the example shown at
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/bitmaps
string resourceID = "GraphicsTest.Android.Media.MyImage.png";
Assembly assembly = GetType().GetTypeInfo().Assembly;

using (Stream stream = assembly.GetManifestResourceStream(resourceID))
{
    MyBitmap = SKBitmap.Decode(stream); // exception thrown here
}

An exception is thrown - "System.ArgumentNullException: 'Value cannot be null'
indicating that the stream object is null.

What I have tried:

I have tried locating the .png file under 'Resources' folder.
I have tried variations of the resourceID string.
I always get the same exception.
Posted
Updated 8-Nov-21 7:32am

1 solution

After playing around with this I found the problem -
The above code was located in
MainPage()
following
InitializeComponent()
It compiled fine, but threw the exception at runtime.
When I moved the code to the event handler used to refresh the Skiasharp canvas, it worked okay.
Since I don't want to recreate the bitmap each time the event handler runs, I have to check to see if the bitmap is null first.
Can't figure out why that should be the case, but it is what it is.
Any explanation?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900