在Xamarin中将SkiaSharp.SKBitmap图像用作Button的ImageSource

10

我正尝试将 SKBitmap 图像作为/转换为 ImageSource 对象,以便通过分配给其 ImageSource 属性在 Button 中使用先前提到的图像,但是就我而言,我无法弄清楚如何将 SKBitmap 对象转换为 ImageSource

搜索网络只会出现有关将 ImageSource 转换为 SKBitmap 的文章/问题,但没有相反的情况。

提前致谢。


1
你不能直接这样做。你可以使用AsPNG/AsJPEG将其写入文件并使用FileImageSource,或者将其转换为SKPixMap并编码为PNG/JPG,然后使用流创建图像源。 - Jason
1
能行吗? - Leo Zhu
3个回答

13

您可以尝试这个:

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);

5

1
您可以尝试使用 NuGet SkiaSharp.Views.WPF,类似于:
...
using BarcodeStandard;
using SkiaSharp;
using SkiaSharp.Views.WPF;
...
SKImage image = new Barcode().Encode(Type.Code128A, text, width, height);
...
ImageSource imageSource = WPFExtensions.ToWriteableBitmap(image);
...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接