예제 #1
0
        public static SKBitmap ToSKBitmap(this CIImage ciImage)
        {
            var extent = ciImage.Extent;
            var info   = new SKImageInfo((int)extent.Width, (int)extent.Height);
            var image  = new SKBitmap(info);

            using (var pixmap = image.PeekPixels())
            {
                ciImage.ToSKPixmap(image.PeekPixels());
            }
            return(image);
        }
예제 #2
0
        public void SwizzleSwapsRedAndBlue()
        {
            var info = new SKImageInfo(10, 10);

            using (var bmp = new SKBitmap(info))
            {
                bmp.Erase(SKColors.Red);

                Assert.Equal(SKColors.Red, bmp.PeekPixels().GetPixelColor(0, 0));

                SKSwizzle.SwapRedBlue(bmp.GetPixels(out var length), info.Width * info.Height);

                Assert.Equal(SKColors.Blue, bmp.PeekPixels().GetPixelColor(0, 0));
            }
        }
        private static Pixel[] GetLinearPixels(SKImage image)
        {
            var height = image.Height;
            var width  = image.Width;

            var linearPixels = ArrayPool <Pixel> .Shared.Rent(width *height);

            using (var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888)))
            {
                using (var canvas = new SKCanvas(bitmap))
                {
                    canvas.DrawImage(image, 0, 0);
                }

                using (var pixmap = bitmap.PeekPixels())
                {
                    var pixels = pixmap.GetPixelSpan <SKColor>();
                    for (var i = 0; i < pixels.Length; i++)
                    {
                        var pixel = pixels[i];
                        linearPixels[i] = new Pixel(
                            SrgbToLinear(pixel.Red),
                            SrgbToLinear(pixel.Green),
                            SrgbToLinear(pixel.Blue));
                    }
                }
            }

            return(linearPixels);
        }
예제 #4
0
 public static Bitmap ToBitmap(this SKBitmap skiaBitmap)
 {
     using (var pixmap = skiaBitmap.PeekPixels())
     {
         return(pixmap.ToBitmap());
     }
 }
예제 #5
0
 public static System.Drawing.Bitmap ToBitmap(this SKBitmap skiaBitmap)
 {
     using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
     {
         return(image.ToBitmap());
     }
 }
예제 #6
0
        public Stream GetBitmap(double nHour, double nMinute, double nSecond, int width = 512, int height = 512, bool bDrawBackImage = false)
        {
            if (bitmap == null || bitmap.Width != width || bitmap.Height != height)
            {
                bitmap?.Dispose();
                bitmap = new SKBitmap(width, height);
            }

            SKCanvas canvas = new SKCanvas(bitmap);

            DrawCanvas(canvas, nHour, nMinute, nSecond, width, height, bDrawBackImage);

            // create an image COPY
            //SKImage image = SKImage.FromBitmap(bitmap);
            // OR
            // create an image WRAPPER
            SKImage image = SKImage.FromPixels(bitmap.PeekPixels());

            // encode the image (defaults to PNG)
            SKData encoded = image.Encode();

            // get a stream over the encoded data
            Stream stream = encoded.AsStream();

            return(stream);
        }
예제 #7
0
        public void Resize(DpiPath dpi, string destination)
        {
            var originalSize = GetOriginalSize();

            var(scaledSize, scale) = GetScaledSize(originalSize, dpi.Scale);

            var sw = new Stopwatch();

            sw.Start();

            // Allocate
            using (var tempBitmap = new SKBitmap(scaledSize.Width, scaledSize.Height))
            {
                // Draw (copy)
                using (var canvas = new SKCanvas(tempBitmap))
                {
                    canvas.Clear(SKColors.Transparent);
                    canvas.Save();
                    canvas.Scale(scale, scale);
                    DrawUnscaled(canvas);
                }

                // Save (encode)
                using (var pixmap = tempBitmap.PeekPixels())
                    using (var wrapper = new SKFileWStream(destination))
                    {
                        pixmap.Encode(wrapper, SKPngEncoderOptions.Default);
                    }
            }

            sw.Stop();
            Logger?.Log($"Save Image took {sw.ElapsedMilliseconds}ms ({destination})");
        }
예제 #8
0
        public void ImmutableBitmapsAreNotCopied()
        {
            // create "pixel data"
            var pixelData = new SKBitmap(100, 100);

            pixelData.Erase(SKColors.Red);

            // create text bitmap
            var bitmap = new SKBitmap();

            bitmap.InstallPixels(pixelData.PeekPixels());

            // mark it as immutable
            bitmap.SetImmutable();

            // create an image
            var image = SKImage.FromBitmap(bitmap);

            Assert.Equal(SKColors.Red, image.PeekPixels().GetPixelColor(50, 50));

            // modify the "pixel data"
            pixelData.Erase(SKColors.Blue);

            // ensure that the pixels are modified
            Assert.Equal(SKColors.Blue, image.PeekPixels().GetPixelColor(50, 50));
        }
예제 #9
0
        private IActionResult ImageToFileStream(SKBitmap bitmap)
        {
            using var pixels = bitmap.PeekPixels();
            var data = pixels.Encode(JpegEncoderOptions);

            return(File(data.AsStream(true), MediaTypeNames.Image.Jpeg));
        }
예제 #10
0
        public void CanScalePixels()
        {
            var srcInfo = new SKImageInfo(200, 200);
            var dstInfo = new SKImageInfo(100, 100);

            var srcSurface = SKSurface.Create(srcInfo);
            var dstBmp     = new SKBitmap(dstInfo);

            using (var paint = new SKPaint {
                Color = SKColors.Green
            })
            {
                srcSurface.Canvas.Clear(SKColors.Blue);
                srcSurface.Canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
            }

            var srcImage = srcSurface.Snapshot();
            var srcPix   = srcImage.PeekPixels();
            var dstPix   = dstBmp.PeekPixels();

            Assert.Equal(SKColors.Green, srcPix.GetPixelColor(75, 75));
            Assert.Equal(SKColors.Blue, srcPix.GetPixelColor(175, 175));

            Assert.True(srcImage.ScalePixels(dstPix, SKFilterQuality.High));

            Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
            Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
        }
예제 #11
0
 public static Pixbuf ToPixbuf(this SKBitmap skiaBitmap)
 {
     using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
     {
         return(image.ToPixbuf());
     }
 }
예제 #12
0
        public void CanScalePixels()
        {
            var srcInfo = new SKImageInfo(200, 200);
            var dstInfo = new SKImageInfo(100, 100);

            var srcBmp = new SKBitmap(srcInfo);
            var dstBmp = new SKBitmap(dstInfo);

            using (var canvas = new SKCanvas(srcBmp))
                using (var paint = new SKPaint {
                    Color = SKColors.Green
                })
                {
                    canvas.Clear(SKColors.Blue);
                    canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
                }

            Assert.Equal(SKColors.Green, srcBmp.GetPixel(75, 75));
            Assert.Equal(SKColors.Blue, srcBmp.GetPixel(175, 175));

            var srcPix = srcBmp.PeekPixels();
            var dstPix = dstBmp.PeekPixels();

            Assert.True(srcPix.ScalePixels(dstPix, SKFilterQuality.High));

            Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
            Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
        }
예제 #13
0
 public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap)
 {
     using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
     {
         return(image.ToWriteableBitmap());
     }
 }
예제 #14
0
        public void CreateThumbnailsNetStandard2()
        {
            //ExStart
            //ExFor:Document.RenderToScale
            //ExSummary:Renders individual pages to graphics to create one image with thumbnails of all pages (.NetStandard 2.0).
            Document doc = new Document(MyDir + "Rendering.docx");

            // Calculate the number of rows and columns that we will fill with thumbnails.
            const int thumbnailColumnsNum = 2;
            int       thumbRows           = Math.DivRem(doc.PageCount, thumbnailColumnsNum, out int remainder);

            if (remainder > 0)
            {
                thumbRows++;
            }

            // Scale the thumbnails relative to the size of the first page.
            const float scale     = 0.25f;
            Size        thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96);

            // Calculate the size of the image that will contain all the thumbnails.
            int imgWidth  = thumbSize.Width * thumbnailColumnsNum;
            int imgHeight = thumbSize.Height * thumbRows;

            using (SKBitmap bitmap = new SKBitmap(imgWidth, imgHeight))
            {
                using (SKCanvas canvas = new SKCanvas(bitmap))
                {
                    // Fill the background, which is transparent by default, in white.
                    canvas.Clear(SKColors.White);

                    for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
                    {
                        int rowIdx = Math.DivRem(pageIndex, thumbnailColumnsNum, out int columnIdx);

                        // Specify where we want the thumbnail to appear.
                        float thumbLeft = columnIdx * thumbSize.Width;
                        float thumbTop  = rowIdx * thumbSize.Height;

                        SizeF size = doc.RenderToScale(pageIndex, canvas, thumbLeft, thumbTop, scale);

                        // Render a page as a thumbnail, and then frame it in a rectangle of the same size.
                        SKRect rect = new SKRect(0, 0, size.Width, size.Height);
                        rect.Offset(thumbLeft, thumbTop);
                        canvas.DrawRect(rect, new SKPaint
                        {
                            Color = SKColors.Black,
                            Style = SKPaintStyle.Stroke
                        });
                    }

                    using (SKFileWStream fs = new SKFileWStream(ArtifactsDir + "Rendering.CreateThumbnailsNetStandard2.png"))
                    {
                        bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                    }
                }
            }
            //ExEnd
        }
예제 #15
0
        public static SKBitmap ToSKBitmap(this Bitmap bitmap)
        {
            var info = GetInfo(bitmap);
            var bmp  = new SKBitmap(info);

            bitmap.ToSKPixmap(bmp.PeekPixels());
            return(bmp);
        }
예제 #16
0
 public override void Save(Stream stream, ImageFormat format, int quality = 100)
 {
     using var wstream = new SKManagedWStream(stream, false);
     using var pixmap  = new SKPixmap();
     if (skBitmap.PeekPixels(pixmap))
     {
         pixmap.Encode(wstream, (SKEncodedImageFormat)format, quality);
     }
 }
예제 #17
0
        public void MismatchingColorTypesThrow()
        {
            var info = new SKImageInfo(1, 1, SKColorType.Rgba8888);

            using var bmp    = new SKBitmap(info);
            using var pixmap = bmp.PeekPixels();

            Assert.Throws <ArgumentException>(() => pixmap.GetPixelSpan <ushort>());
        }
예제 #18
0
        public void ByteWorksForEverything(SKColorType colortype)
        {
            var info = new SKImageInfo(1, 1, colortype);

            using var bmp    = new SKBitmap(info);
            using var pixmap = bmp.PeekPixels();

            Assert.Equal(info.BytesSize, pixmap?.GetPixelSpan <byte>().Length ?? 0);
        }
예제 #19
0
 public static Bitmap ToBitmap(this SKBitmap skiaBitmap)
 {
     using (var pixmap = skiaBitmap.PeekPixels())
     {
         var bmp = pixmap.ToBitmap();
         GC.KeepAlive(skiaBitmap);
         return(bmp);
     }
 }
예제 #20
0
        /// <summary>
        /// Resize using SkiaSharp - this can do 100 images in about 30s (2020 i5 MacBook Air).
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destFiles"></param>
        public void CreateThumbs(FileInfo source, IDictionary <FileInfo, ThumbConfig> destFiles, out string imageHash)
        {
            try
            {
                int desiredWidth = destFiles.Max(x => x.Value.width);

                using var sourceBitmap = LoadOrientedBitmap(source, desiredWidth);

                imageHash = GetHash(sourceBitmap);

                Stopwatch thumbs = new Stopwatch("SkiaSharpThumbs");

                // Dropping this from High to Low doesn't have that much of an effect
                // in terms of performance.
                var quality   = SKFilterQuality.Low;
                var srcBitmap = sourceBitmap;

                foreach (var pair in destFiles.OrderByDescending(x => x.Value.width))
                {
                    var dest   = pair.Key;
                    var config = pair.Value;

                    float widthScaleFactor = (float)srcBitmap.Width / (float)config.width;
                    float heighScaleFactor = (float)srcBitmap.Height / (float)config.height;
                    float scaleFactor      = Math.Min(widthScaleFactor, heighScaleFactor);

                    using var scaledImage = new SKBitmap((int)(srcBitmap.Width / scaleFactor), (int)(srcBitmap.Height / scaleFactor));
                    srcBitmap.ScalePixels(scaledImage.PeekPixels(), quality);

                    var cropSize = new SKSize {
                        Height = config.height, Width = config.width
                    };
                    using var cropped = config.cropToRatio ? Crop(scaledImage, cropSize) : scaledImage;

                    using SKData data = cropped.Encode(SKEncodedImageFormat.Jpeg, 90);

                    using (var stream = new FileStream(dest.FullName, FileMode.Create, FileAccess.Write))
                        data.SaveTo(stream);

                    // Now, use the previous scaled image as the basis for the
                    // next smaller thumbnail. This should reduce processing
                    // time as we only work on the large image on the first
                    // iteration
                    srcBitmap = scaledImage.Copy();

                    // TODO: Dispose
                }

                thumbs.Stop();
            }
            catch (Exception ex)
            {
                Logging.Log($"Exception during Skia processing: {ex.Message}");
                throw;
            }
        }
예제 #21
0
 public static Pixbuf ToPixbuf(this SKBitmap skiaBitmap)
 {
     using (var pixmap = skiaBitmap.PeekPixels())
         using (var image = SKImage.FromPixels(pixmap))
         {
             var pixbuf = image.ToPixbuf();
             GC.KeepAlive(skiaBitmap);
             return(pixbuf);
         }
 }
예제 #22
0
 public static System.Drawing.Bitmap ToBitmap(this SKBitmap skiaBitmap)
 {
     using (var pixmap = skiaBitmap.PeekPixels())
         using (var image = SKImage.FromPixels(pixmap))
         {
             var bmp = image.ToBitmap();
             GC.KeepAlive(skiaBitmap);
             return(bmp);
         }
 }
예제 #23
0
 public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap)
 {
     using (var pixmap = skiaBitmap.PeekPixels())
         using (var image = SKImage.FromPixels(pixmap))
         {
             var wb = image.ToWriteableBitmap();
             GC.KeepAlive(skiaBitmap);
             return(wb);
         }
 }
예제 #24
0
        private static byte[] ToPngData(SKBitmap bitmap)
        {
            var pixels = bitmap.PeekPixels();

            using (var resultData = pixels.Encode(SKPngEncoderOptions.Default))
            {
                var ms = new MemoryStream();
                resultData.AsStream().CopyTo(ms);
                return(ms.ToArray());
            }
        }
예제 #25
0
        public void GetPixelSpanReadsGray8Correctly(uint rgb888, byte gray8)
        {
            var info = new SKImageInfo(1, 1, SKColorType.Gray8);

            using var bmp    = new SKBitmap(info);
            using var pixmap = bmp.PeekPixels();

            pixmap.Erase(rgb888);

            Assert.Equal(gray8, pixmap.GetPixelSpan <byte>()[0]);
        }
예제 #26
0
        public void GetPixelSpanReads565Correctly(uint rgb888, ushort rgb565)
        {
            var info = new SKImageInfo(1, 1, SKColorType.Rgb565);

            using var bmp    = new SKBitmap(info);
            using var pixmap = bmp.PeekPixels();

            pixmap.Erase(rgb888);

            Assert.Equal(rgb565, pixmap.GetPixelSpan <ushort>()[0]);
        }
예제 #27
0
        public void EraseWithColorF()
        {
            var info = new SKImageInfo(1, 1);

            using var bmp    = new SKBitmap(info);
            using var pixmap = bmp.PeekPixels();

            pixmap.Erase(new SKColorF(1, 0, 0));

            Assert.Equal(SKColors.Red, pixmap.GetPixelColor(0, 0));
        }
예제 #28
0
        public static SKBitmap ToSKBitmap(this BitmapSource bitmap)
        {
            var info       = new SKImageInfo(bitmap.PixelWidth, bitmap.PixelHeight);
            var skiaBitmap = new SKBitmap(info);

            using (var pixmap = skiaBitmap.PeekPixels())
            {
                bitmap.ToSKPixmap(pixmap);
            }
            return(skiaBitmap);
        }
예제 #29
0
        public static SKBitmap ToSKBitmap(this CGImage cgImage)
        {
            var info   = new SKImageInfo((int)cgImage.Width, (int)cgImage.Height);
            var bitmap = new SKBitmap(info);

            using (var pixmap = bitmap.PeekPixels())
            {
                cgImage.ToSKPixmap(pixmap);
            }
            return(bitmap);
        }
        public static Image GetImageFromBitmap(SKBitmap bitmap)
        {
            Image img = new Image();

            SKImage image   = SKImage.FromPixels(bitmap.PeekPixels());
            SKData  encoded = image.Encode();
            Stream  stream  = encoded.AsStream();

            img.Source = ImageSource.FromStream(() => stream);

            return(img);
        }