/// <summary> /// Create new writeable bitmap. /// </summary> /// <param name="size">The size of the bitmap in device pixels.</param> /// <param name="dpi">The DPI of the bitmap.</param> /// <param name="format">The pixel format.</param> /// <param name="alphaFormat">The alpha format.</param> public WriteableBitmapImpl(PixelSize size, Vector dpi, PixelFormat format, AlphaFormat alphaFormat) { PixelSize = size; Dpi = dpi; SKColorType colorType = format.ToSkColorType(); SKAlphaType alphaType = alphaFormat.ToSkAlphaType(); var runtimePlatform = AvaloniaLocator.Current?.GetService <IRuntimePlatform>(); if (runtimePlatform != null) { _bitmap = new SKBitmap(); var nfo = new SKImageInfo(size.Width, size.Height, colorType, alphaType); var blob = runtimePlatform.AllocBlob(nfo.BytesSize); _bitmap.InstallPixels(nfo, blob.Address, nfo.RowBytes, s_releaseDelegate, blob); } else { _bitmap = new SKBitmap(size.Width, size.Height, colorType, alphaType); } _bitmap.Erase(SKColor.Empty); }
/// <summary> /// Create immutable bitmap from given pixel data copy. /// </summary> /// <param name="size">Size of the bitmap.</param> /// <param name="dpi">DPI of the bitmap.</param> /// <param name="stride">Stride of data pixels.</param> /// <param name="format">Format of data pixels.</param> /// <param name="alphaFormat">Alpha format of data pixels.</param> /// <param name="data">Data pixels.</param> public ImmutableBitmap(PixelSize size, Vector dpi, int stride, PixelFormat format, AlphaFormat alphaFormat, IntPtr data) { var imageInfo = new SKImageInfo(size.Width, size.Height, format.ToSkColorType(), alphaFormat.ToSkAlphaType()); _image = SKImage.FromPixelCopy(imageInfo, data, stride); if (_image == null) { throw new ArgumentException("Unable to create bitmap from provided data"); } PixelSize = size; Dpi = dpi; }