/// <summary> /// Copies the pixel data to bitmap, data assumed to be in ARGB format so it will be trasformed to the internal Android ABGR format that bitmaps use while copying. /// </summary> /// <param name="targetBitmap">The target bitmap.</param> /// <param name="data">The data.</param> /// <param name="stride">The stride, bitmap width in pixels.</param> public static void CopyPixelDataToBitmap(Bitmap targetBitmap, int[] data, int stride) { if (targetBitmap != null) { IntPtr ptr = targetBitmap.LockPixels(); unchecked { int value; int[] strideArray = new int[stride]; for (int i = 0; i < data.Length; i += stride) { for (int j = 0, k = i; j < stride; ++j, ++k) { value = data[k]; strideArray[j] = (int) ((0xFF000000) | ((value & 0xFF) << 16) | (value & 0x0000FF00) | ((value >> 16) & 0xFF)); } Marshal.Copy(strideArray, 0, ptr + (i << 2), stride); } } targetBitmap.UnlockPixels(); } }
private static void _PerformCopy(SpanBitmap src, ANDROIDBITMAP dst) { if (src.Info.IsEmpty) { return; } if (dst == null) { return; } if (dst.IsRecycled || !dst.IsMutable) { throw new ArgumentNullException(nameof(dst)); } if (!TryGetBitmapInfo(dst.GetBitmapInfo(), dst.IsPremultiplied, out var nfo)) { throw new Diagnostics.PixelFormatNotSupportedException((dst.GetBitmapInfo().Format, dst.Premultiplied), nameof(dst)); } var ptr = dst.LockPixels(); if (ptr == IntPtr.Zero) { throw new ArgumentNullException("lock", nameof(dst)); } try { new PointerBitmap(ptr, nfo).AsSpanBitmap().SetPixels(0, 0, src); } finally { dst.UnlockPixels(); } }
public static bool CopyTo(ANDROIDBITMAP src, ref MemoryBitmap dst) { var info = src.GetBitmapInfo().ToInterop(); var ptr = src.LockPixels(); try { return(new PointerBitmap(ptr, info).CopyTo(ref dst)); } finally { src.UnlockPixels(); } }
public static void Mutate(ANDROIDBITMAP bmp, Action <PointerBitmap> pinContext) { System.Diagnostics.Debug.Assert(bmp != null); if (!TryGetBitmapInfo(bmp.GetBitmapInfo(), bmp.IsPremultiplied, out var info)) { throw new Diagnostics.PixelFormatNotSupportedException(bmp.GetBitmapInfo(), nameof(bmp)); } if (info.BitmapByteSize != bmp.ByteCount) { throw new InvalidOperationException("Byte Size mismatch"); } var ptr = bmp.LockPixels(); try { pinContext(new PointerBitmap(ptr, info, !bmp.IsMutable)); } finally { bmp.UnlockPixels(); } }
protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); // bail out if the view is not actually visible if (Visibility != ViewStates.Visible) { FreeBitmap(); return; } // create a skia surface var surface = CreateSurface(out var info); if (surface == null) { return; } // draw using SkiaSharp element?.OnPaintSurface(surface, info); // draw the surface to the view // clean up skia objects surface.Canvas.Flush(); surface.Dispose(); // get the bitmap ready for drawing bitmap.UnlockPixels(); // get the bounds var src = new Rect(0, 0, info.Width, info.Height); var dst = new RectF(0, 0, canvas.Width, canvas.Height); // draw bitmap to the view canvas canvas.DrawBitmap(bitmap, src, dst, null); }