如何将SkiaSharp.SkBitmap转换为Android.Graphics.Bitmap?



我想将PinBitmap(SkiaSharp.SkBitmap(转换为Android.Graphics.Bitmap。我找不到在线参考,我只在 Android 项目中尝试过这个:

Android.Graphics.Bitmap bitmap = BitmapFactory.DecodeByteArray(myView.PinBitmap.Bytes, 0, myView.PinBitmap.Bytes.Length);

bitmap为空。

我正在从SKCanvasView创建PinBitmap

private void SKCanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
{
var surface = e.Surface;
var canvas = surface.Canvas;
SKImageInfo info = e.Info;
canvas.DrawLine(10, 10, 10, 200, new SKPaint() { IsStroke = true, Color = SKColors.Green, StrokeWidth = 10 });
SKBitmap saveBitmap = new SKBitmap();
// Create bitmap the size of the display surface
if (saveBitmap == null)
{
saveBitmap = new SKBitmap(info.Width, info.Height);
}
// Or create new bitmap for a new size of display surface
else if (saveBitmap.Width < info.Width || saveBitmap.Height < info.Height)
{
SKBitmap newBitmap = new SKBitmap(Math.Max(saveBitmap.Width, info.Width),
Math.Max(saveBitmap.Height, info.Height));
using (SKCanvas newCanvas = new SKCanvas(newBitmap))
{
newCanvas.Clear();
newCanvas.DrawBitmap(saveBitmap, 0, 0);
}
saveBitmap = newBitmap;
}
// Render the bitmap
canvas.Clear();
canvas.DrawBitmap(saveBitmap, 0, 0);
var customPin = new CustomPin { PinBitmap = saveBitmap };
Content = customPin;
}

这很容易做到,你只需要拥有SkiaSharp.Views NuGet 包已安装。然后,有扩展方法:

skiaBitmap = androidBitmap.ToSKBitmap(); 
androidBitmap = skiaBitmap.ToBitmap(); 

还有一些其他的,比如:ToSKImage 和 ToSKPixmap。

注意:这些都制作了像素数据的副本。为避免内存问题, 您可以在方法返回后立即处理原始文件。

来源: https://forums.xamarin.com/discussion/comment/294868/#Comment_294868

我想将 PinBitmap (SkiaSharp.SkBitmap( 转换为 Android.Graphics.Bitmap

在Android MainActivity中,您可以使用AndroidExtensions.ToBitmap方法将PinBitmap转换为Bitmap。

AndroidExtensions.ToBitmap方法:https://learn.microsoft.com/en-us/dotnet/api/skiasharp.views.android.androidextensions.tobitmap?view=skiasharp-views-1.68.1

从 NuGet 包安装SkiaSharp.Views.Forms。 https://www.nuget.org/packages/SkiaSharp.Views.Forms/

使用引用。

using SkiaSharp.Views.Android;

使用以下代码。

var bitmap = AndroidExtensions.ToBitmap(PinBitmap);

相关内容

最新更新