示例#1
0
        public static SKBitmap ResizeBitmap(
            [InputPin(PropertyMode = PropertyMode.Never)] SKBitmap bitmap,
            [InputPin(PropertyMode = PropertyMode.Default)] int width  = 256,
            [InputPin(PropertyMode = PropertyMode.Default)] int height = 256,
            [InputPin(PropertyMode = PropertyMode.Default)] SKBitmapResizeMethod method = SKBitmapResizeMethod.Lanczos3
            )
        {
            if (bitmap.ColorType != SKImageInfo.PlatformColorType)
            {
                using (var platformBitmap = new SKBitmap())
                {
                    if (!bitmap.CopyTo(platformBitmap, SKImageInfo.PlatformColorType))
                    {
                        throw new Exception($"Could not convert input image ({bitmap.ColorType}) to SKIA platform color type ({SKImageInfo.PlatformColorType}).");
                    }

                    var destinationInfo = platformBitmap.Info;
                    destinationInfo.Width  = width;
                    destinationInfo.Height = height;
                    return(platformBitmap.Resize(destinationInfo, method));
                }
            }
            else
            {
                SKBitmap result = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType);
                bitmap.Resize(result, method);
                return(result);
            }
        }
示例#2
0
 private static void ConvertBitmapIfNecessary(SKBitmap bitmap)
 {
     if (IsColorTypeSupported(bitmap.ColorType))
     {
         return;
     }
     bitmap.CopyTo(bitmap, GetPlatformColorType());
 }
示例#3
0
        public static Bitmap ToBitmap(this SKBitmap skiaBitmap)
        {
            // TODO: replace all this with a SKBitmap.PeekPixels
            //       and call the overload

            var info = skiaBitmap.Info;

            // try keep the pixel format
            var config = Bitmap.Config.Argb8888;

            switch (info.ColorType)
            {
            case SKColorType.Alpha8:
                config = Bitmap.Config.Alpha8;
                break;

            case SKColorType.Rgb565:
                config = Bitmap.Config.Rgb565;
                break;

            case SKColorType.Argb4444:
                config = Bitmap.Config.Argb4444;
                break;
            }

            var bmp     = Bitmap.CreateBitmap(info.Width, info.Height, config);
            var ptr     = bmp.LockPixels();
            var success = true;

            if (config == Bitmap.Config.Argb8888 && info.ColorType != SKColorType.Rgba8888)
            {
                // wrap the pixels so we can copy in one action
                var tempInfo = new SKImageInfo(info.Width, info.Height, SKColorType.Rgba8888, SKAlphaType.Premul);
                // TODO: replace this with a SKPixmap.ReadPixels
                var tempBmp = new SKBitmap();
                tempBmp.InstallPixels(tempInfo, ptr);
                success = skiaBitmap.CopyTo(tempBmp, SKColorType.Rgba8888);
                tempBmp.Reset();
                tempBmp.Dispose();
            }
            else
            {
                // must multiply HEIGHT * rowBytes to get total number of bytes
                success = skiaBitmap.CopyPixelsTo(ptr, bmp.Height * bmp.RowBytes);
            }

            bmp.UnlockPixels();
            if (!success)
            {
                bmp.Recycle();
                bmp.Dispose();
                bmp = null;
            }

            return(bmp);
        }
 /// <summary>Initializes a new instance of the <see cref="Bitmap32"/> class.</summary>
 /// <param name="bitmap">The bitmap.</param>
 public SkiaBitmap32(SKBitmap bitmap)
     : base()
 {
     if (bitmap.ColorType != SKImageInfo.PlatformColorType)
     {
         //change colortype
         SKBitmap bmp = new SKBitmap(bitmap.Width, bitmap.Height, SKImageInfo.PlatformColorType, SKAlphaType.Unpremul);
         bitmap.CopyTo(bmp, SKImageInfo.PlatformColorType);
         bitmap = bmp;
     }
     this.bitmap = bitmap ?? throw new ArgumentNullException("bitmap");
 }
示例#5
0
        public void Export(string path, int width, int height)
        {
            if (!File.Exists(path))
            {
                Log.Write($"[{this.Path} ({this.bitmap.Width}x{this.bitmap.Height})({this.Density}x)] -> Generating [{path} ({width}x{height})]");

                // SKBitmap.Resize() doesn't support SKColorType.Index8
                // https://github.com/mono/SkiaSharp/issues/331
                if (bitmap.ColorType != SKImageInfo.PlatformColorType)
                {
                    bitmap.CopyTo(bitmap, SKImageInfo.PlatformColorType);
                }

                var info = new SKImageInfo(width, height);

                using (var resized = bitmap.Resize(info, SKBitmapResizeMethod.Lanczos3))
                {
                    if (resized == null)
                    {
                        throw new InvalidOperationException($"Failed to resize : {Path}.");
                    }

                    using (var image = SKImage.FromBitmap(resized))
                        using (var data = image.Encode())
                        {
                            path.CreateParentDirectory();

                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }

                            using (var fileStream = File.Create(path))
                                using (var outputStream = data.AsStream())
                                {
                                    outputStream.CopyTo(fileStream);
                                }
                        }
                }
            }
            else
            {
                Log.Write($"[{this.Path} ({this.bitmap.Width}x{this.bitmap.Height})({this.Density}x)] -> Didn't generate [{path} ({width}x{height})] because it already exists.");
            }
        }
示例#6
0
        public void Export(string path, int width, int height)
        {
            if (File.Exists(path) && File.GetLastWriteTime(path) >= File.GetLastWriteTime(Path))
            {
                Log.Write($"[{Path} ({_bitmap.Width}x{_bitmap.Height})({Density}x)] -> Didn't generate [{path} ({width}x{height})] because it already exists and hasn't been updated.");
            }
            else
            {
                Log.Write($"[{Path} ({_bitmap.Width}x{_bitmap.Height})({Density}x)] -> Generating [{path} ({width}x{height})].");

                if (_bitmap.ColorType != SKImageInfo.PlatformColorType)
                {
                    _bitmap.CopyTo(_bitmap, SKImageInfo.PlatformColorType);
                }

                var info = new SKImageInfo(width, height);

                using (var resized = _bitmap.Resize(info, SKFilterQuality.High))
                {
                    if (resized == null)
                    {
                        throw new InvalidOperationException($"Failed to resize: {Path}.");
                    }

                    using (var image = SKImage.FromBitmap(resized))
                        using (var data = image.Encode())
                        {
                            path.CreateParentDirectory();

                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }

                            using (var fileStream = File.Create(path))
                                using (var outputStream = data.AsStream())
                                    outputStream.CopyTo(fileStream);
                        }
                }
            }

            Log.Write($"AssetManGeneratedFile({path})");
        }