SkiaSharp Platform Specific Details
- PDF for offline use:
- Interactive:
Let us know how you feel about this.
0/250
last updated: 2016-02
Provide details on how to bring your SkSurface contents into your native application.
Platform Specific Integration
The examples below allocate the image buffers manually, this is done to illustrate a common platform pattern which is to draw on a existing RBGA buffer provided by the platform.
You do not need to use this idiom if you do not want to. There is an overload that will create and manage the backing storage for your image for you.
iOS
var screenScale = UIScreen.MainScreen.Scale;
var width = (int)(Bounds.Width * screenScale);
var height = (int)(Bounds.Height * screenScale);
IntPtr buff = System.Runtime.InteropServices.Marshal.AllocCoTaskMem (width * height * 4);
try {
using (var surface = SKSurface.Create (width, height, SKColorType.N_32, SKAlphaType.Premul, buff, width * 4)) {
var skcanvas = surface.Canvas;
skcanvas.Scale ((float)screenScale, (float)screenScale);
using (new SKAutoCanvasRestore (skcanvas, true)) {
// DoDraw (skcanvas);
}
}
using (var colorSpace = CGColorSpace.CreateDeviceRGB ())
using (var bContext = new CGBitmapContext (buff, width, height, 8, width * 4, colorSpace, (CGImageAlphaInfo)bitmapInfo))
using (var image = bContext.ToImage ())
using (var context = UIGraphics.GetCurrentContext ()) {
// flip the image for CGContext.DrawImage
context.TranslateCTM (0, Frame.Height);
context.ScaleCTM (1, -1);
context.DrawImage (Bounds, image);
}
} finally {
if (buff != IntPtr.Zero) {
System.Runtime.InteropServices.Marshal.FreeCoTaskMem (buff);
}
}
Android
var width = (float)skiaView.Width;
var height = (float)skiaView.Height;
using (var bitmap = Bitmap.CreateBitmap (canvas.Width, canvas.Height, Bitmap.Config.Argb8888)) {
try {
using (var surface = SKSurface.Create (canvas.Width, canvas.Height, SKColorType.Rgba_8888, SKAlphaType.Premul, bitmap.LockPixels (), canvas.Width * 4)) {
var skcanvas = surface.Canvas;
skcanvas.Scale (((float)canvas.Width)/width, ((float)canvas.Height)/height);
// DoDraw (skcanvas);
}
} finally {
bitmap.UnlockPixels ();
}
canvas.DrawBitmap (bitmap, 0, 0, null);
}
OS X
var screenScale = (int)NSScreen.MainScreen.BackingScaleFactor * 2;
var width = (int)Bounds.Width * screenScale;
var height = (int)Bounds.Height * screenScale;
IntPtr buff = System.Runtime.InteropServices.Marshal.AllocCoTaskMem (width * height * 4);
try {
using (var surface = SKSurface.Create (width, height, SKColorType.Rgba_8888, SKAlphaType.Premul, buff, width * 4)) {
var skcanvas = surface.Canvas;
skcanvas.Scale (screenScale, screenScale);
// DoDraw (skcanvas);
}
int flag = ((int)CoreGraphics.CGBitmapFlags.ByteOrderDefault) | ((int)CoreGraphics.CGImageAlphaInfo.PremultipliedLast);
using (var colorSpace = CoreGraphics.CGColorSpace.CreateDeviceRGB ())
using (var bContext = new CoreGraphics.CGBitmapContext (buff, width, height, 8, width * 4, colorSpace, (CoreGraphics.CGImageAlphaInfo) flag))
using (var image = bContext.ToImage ())
using (var context = NSGraphicsContext.CurrentContext.GraphicsPort) {
context.DrawImage (Bounds, image);
}
} finally {
if (buff != IntPtr.Zero) {
System.Runtime.InteropServices.Marshal.FreeCoTaskMem (buff);
}
}
Windows Desktop / Mac Desktop
var width = Width;
var height = Height;
using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb)) {
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
using (var surface = SKSurface.Create(width, height, SKColorType.N_32, SKAlphaType.Premul, data.Scan0, width * 4)) {
var skcanvas = surface.Canvas;
// DoDraw (skcanvas);
}
bitmap.UnlockBits(data);
e.Graphics.DrawImage(bitmap, new Rectangle(0, 0, Width, Height));
}
Let us know how you feel about this.
0/250
Xamarin Workbook
If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.
