Using SkiaSharp.SKBitmap image as the ImageSource for a Button in Xamarin
Asked Answered
R

3

10

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.

Racon answered 6/8, 2020 at 23:27 Comment(2)
you can't do this directly. You could use AsPNG/AsJPEG to write it to a file and use FileImageSource, or convert to SKPixMap and Encode as a PNG/JPG and then use a stream to create an image sourceCrustacean
Could it work ?Jessalin
R
6

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;
Racon answered 14/8, 2020 at 5:27 Comment(0)
J
13

You could try this:

SKBitmap bitmap = ...;
// create an image COPY
//SKImage image = SKImage.FromBitmap(bitmap);
// OR
// create an image WRAPPER
SKImage image = SKImage.FromPixels(bitmap.PeekPixels());
// encode the image (defaults to PNG)
SKData encoded = image.Encode();
// get a stream over the encoded data
Stream stream = encoded.AsStream();
img.Source = ImageSource.FromStream(()=> stream);
Jessalin answered 7/8, 2020 at 2:24 Comment(1)
From docs: "... must return a new stream on every invocation." ImageSource.FromStream must be given a function that resets stream to beginning, then returns it.Demonstrative
R
6

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;
Racon answered 14/8, 2020 at 5:27 Comment(0)
L
1

You could try the NuGet SkiaSharp.Views.WPF like:

...
using BarcodeStandard;
using SkiaSharp;
using SkiaSharp.Views.WPF;
...
SKImage image = new Barcode().Encode(Type.Code128A, text, width, height);
...
ImageSource imageSource = WPFExtensions.ToWriteableBitmap(image);
...

Leveille answered 4/8, 2023 at 6:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.