public WriteableBitmapImpl(PixelSize size, Vector dpi, PixelFormat?format = null)
        {
            PixelSize = size;
            Dpi       = dpi;

            var colorType = PixelFormatHelper.ResolveColorType(format);

            var runtimePlatform = AvaloniaGlobals.RuntimePlatform;

            if (runtimePlatform != null)
            {
                _bitmap = new SKBitmap();

                var nfo  = new SKImageInfo(size.Width, size.Height, colorType, SKAlphaType.Premul);
                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, SKAlphaType.Premul);
            }

            _bitmap.Erase(SKColor.Empty);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create new writeable bitmap.
        /// </summary>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        /// <param name="format">Format.</param>
        public WriteableBitmapImpl(int width, int height, PixelFormat?format = null)
        {
            PixelHeight = height;
            PixelWidth  = width;

            var colorType = PixelFormatHelper.ResolveColorType(format);

            var runtimePlatform = AvaloniaLocator.Current?.GetService <IRuntimePlatform>();

            if (runtimePlatform != null)
            {
                _bitmap = new SKBitmap();

                var nfo  = new SKImageInfo(width, height, colorType, SKAlphaType.Premul);
                var blob = runtimePlatform.AllocBlob(nfo.BytesSize);

                _bitmap.InstallPixels(nfo, blob.Address, nfo.RowBytes, null, s_releaseDelegate, blob);
            }
            else
            {
                _bitmap = new SKBitmap(width, height, colorType, SKAlphaType.Premul);
            }

            _bitmap.Erase(SKColor.Empty);
        }
        /// <summary>
        /// Create image info for given parameters.
        /// </summary>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        /// <param name="format">Format.</param>
        /// <returns></returns>
        private static SKImageInfo MakeImageInfo(int width, int height, PixelFormat?format)
        {
            var colorType = PixelFormatHelper.ResolveColorType(format);

            return(new SKImageInfo(Math.Max(width, 1), Math.Max(height, 1), colorType, SKAlphaType.Premul));
        }