I am attempting to use/convert the SKBitmap
image as/to an ImageSource
object in order to use the aforementioned image in a Button
by assignment to its ImageSource
property but, for the life of me, cannot figure out how to convert an SKBitmap
object to an ImageSource
.
Searching the web only yielded the articles/issues on converting an ImageSource
to SKBitmap
but not the converse.
Thanks in advance.
The ImageSource
property of the Button
class is of ImageSource
type, whose value can be set to that of a string representing the path of an image either via assigning the string to the property or using the ImageSource.FromFile()
method. As the ImageSource
type cannot be used with SKBitmap
images, the image represented by the SKBitmap
object can be saved to the disk (preferably in the application cache) and the path to the saved image can be used to initialize the concerned ImageSource
object.
SKBitmap bitmap;
SKImage image = SKImage.FromBitmap(bitmap);
SKData encodedData = image.Encode(SKEncodedImageFormat.Png, 100);
string imagePath = Path.Combine(FileSystem.CacheDirectory, "image.png");
bitmapImageStream = File.Open(imagePath,
FileMode.Create,
FileAccess.Write,
FileShare.None);
encodedData.SaveTo(bitmapImageStream);
bitmapImageStream.Flush(true);
bitmapImageStream.Dispose();
ImageSource imgSrc;
imgSrc = ImageSource.FromFile(imagePath);
// or imgSrc = imagePath;