如何将SkiaSharp.SkBitmap转换成Android.Graphics.Bitmap?
How to convert SkiaSharp.SkBitmap to 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;
}
This is easy to do, you just need to have the SkiaSharp.Views NuGet
package installed. Then, there are extension methods:
skiaBitmap = androidBitmap.ToSKBitmap();
androidBitmap = skiaBitmap.ToBitmap();
There are also a few others, like: ToSKImage
and ToSKPixmap.
NOTE: these all make copies of the pixel data. To avoid memory issue,
you can dispose of the original as soon as the method returns.
来源:https://forums.xamarin.com/discussion/comment/294868/#Comment_294868
I want to convert PinBitmap (SkiaSharp.SkBitmap) to Android.Graphics.Bitmap
在Android MainActivity中,您可以使用AndroidExtensions.ToBitmap
方法将PinBitmap转换为Bitmap。
AndroidExtensions.ToBitmap
方法:https://docs.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);
我想将 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;
}
This is easy to do, you just need to have the SkiaSharp.Views NuGet package installed. Then, there are extension methods:
skiaBitmap = androidBitmap.ToSKBitmap(); androidBitmap = skiaBitmap.ToBitmap();
There are also a few others, like: ToSKImage and ToSKPixmap.
NOTE: these all make copies of the pixel data. To avoid memory issue, you can dispose of the original as soon as the method returns.
来源:https://forums.xamarin.com/discussion/comment/294868/#Comment_294868
I want to convert PinBitmap (SkiaSharp.SkBitmap) to Android.Graphics.Bitmap
在Android MainActivity中,您可以使用AndroidExtensions.ToBitmap
方法将PinBitmap转换为Bitmap。
AndroidExtensions.ToBitmap
方法:https://docs.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);