public static void RenderRaster(SKCanvas canvas, SKBitmap bitmap, SKRect rect, float opacity = 1f) { // Better for quality. Helps to compare to WPF var color = new SKColor(255, 255, 255, (byte)(255 * opacity)); var paint = new SKPaint { Color = color, FilterQuality = SKFilterQuality.High }; canvas.DrawBitmap(bitmap, rect, paint); // Better for performance: canvas.DrawBitmap(bitmap, rect); }
/// <summary> /// Combines 2 images into one, given the shared ImageStats for both supplied images. /// </summary> /// <param name="info">the ImageStats object used to generate both bottom and top tiles.</param> /// <param name="bottomTile">the tile to use as the base of the image. Expected to be opaque.</param> /// <param name="topTile">The tile to layer on top. Expected to be at least partly transparent or translucent.</param> /// <returns></returns> public byte[] LayerTiles(ImageStats info, byte[] bottomTile, byte[] topTile) { SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap(info.imageSizeX, info.imageSizeY, SkiaSharp.SKColorType.Rgba8888, SkiaSharp.SKAlphaType.Premul); SkiaSharp.SKCanvas canvas = new SkiaSharp.SKCanvas(bitmap); SkiaSharp.SKPaint paint = new SkiaSharp.SKPaint(); canvas.Scale(1, 1, info.imageSizeX / 2, info.imageSizeY / 2); paint.IsAntialias = true; var baseBmp = SkiaSharp.SKBitmap.Decode(bottomTile); var topBmp = SkiaSharp.SKBitmap.Decode(topTile); canvas.DrawBitmap(baseBmp, 0, 0); canvas.DrawBitmap(topBmp, 0, 0); var ms = new MemoryStream(); var skms = new SkiaSharp.SKManagedWStream(ms); bitmap.Encode(skms, SkiaSharp.SKEncodedImageFormat.Png, 100); var output = ms.ToArray(); skms.Dispose(); ms.Close(); ms.Dispose(); return(output); }
public static void BlurImageFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) using (var filter = SKImageFilter.CreateBlur(5, 5)) using (var paint = new SKPaint()) { paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
} // End Sub DrawWithoutSurface public void DrawImageToCanvas(string fileName, SkiaSharp.SKCanvas canvas) { // Clear the canvas / Fill with white canvas.DrawColor(SKColors.White); using (System.IO.Stream fileStream = System.IO.File.OpenRead(fileName)) { // decode the bitmap from the stream using (SKManagedStream stream = new SKManagedStream(fileStream)) { using (SKBitmap bitmap = SKBitmap.Decode(stream)) { using (SKPaint paint = new SKPaint()) { canvas.DrawBitmap(bitmap, SKRect.Create(Width, Height), paint); } // End using paint } // End using bitmap } // End Using stream } // End Using fileStream } // End Sub DrawImageToCanvas
public static void ColorMatrixColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) { var f = new Action <SKRect, float[]>((rect, colorMatrix) => { using (var cf = SKColorFilter.CreateColorMatrix(colorMatrix)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, rect, paint); } }); var colorMatrix1 = new float[20] { 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var grayscale = new float[20] { 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix3 = new float[20] { -1f, 1f, 1f, 0f, 0f, 1f, -1f, 1f, 0f, 0f, 1f, 1f, -1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var colorMatrix4 = new float[20] { 0.0f, 0.5f, 0.5f, 0f, 0f, 0.5f, 0.0f, 0.5f, 0f, 0f, 0.5f, 0.5f, 0.0f, 0f, 0f, 0.0f, 0.0f, 0.0f, 1f, 0f }; var highContrast = new float[20] { 4.0f, 0.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 4.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 4.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix6 = new float[20] { 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var sepia = new float[20] { 0.393f, 0.769f, 0.189f, 0.0f, 0.0f, 0.349f, 0.686f, 0.168f, 0.0f, 0.0f, 0.272f, 0.534f, 0.131f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var inverter = new float[20] { -1f, 0f, 0f, 0f, 255f, 0f, -1f, 0f, 0f, 255f, 0f, 0f, -1f, 0f, 255f, 0f, 0f, 0f, 1f, 0f }; var matices = new[] { colorMatrix1, grayscale, highContrast, sepia, colorMatrix3, colorMatrix4, colorMatrix6, inverter }; var cols = width < height ? 2 : 4; var rows = (matices.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { f(SKRect.Create(x * w, y * h, w, h), matices[y * cols + x]); } } } }
public void DrawBitmap(IBitmap bitmap, int width, int height) => _canvas.DrawBitmap(bitmap.ToSkia(), width, height);
protected override void InternalDraw(SkiaSharp.SKCanvas canvas) { base.InternalDraw(canvas); using (var paint = new SKPaint()) { var renderBounds = RenderBounds; var smallestSize = Math.Min(renderBounds.Size.Width, renderBounds.Size.Height); var square = new Rectangle(renderBounds.Center.X - smallestSize / 2.0, renderBounds.Center.Y - smallestSize / 2.0, smallestSize, smallestSize); Rectangle imageBounds = renderBounds; paint.IsAntialias = true; if (IsCheckable) { paint.Color = SelectionBackgroundColor.ToSKColor(); canvas.DrawOval(square.ToSKRect(), paint); var darkerOutline = SelectionBackgroundColor.AddLuminosity(-0.1); paint.IsStroke = true; paint.Color = darkerOutline.ToSKColor(); paint.StrokeWidth = 1.0f; canvas.DrawOval(square.ToSKRect(), paint); imageBounds = renderBounds.Inflate(-BorderSize, -BorderSize); } if (Drawable != null && !IsSelected) { var drawableSize = Drawable.GetSize(imageBounds.Width, imageBounds.Height); double ratioX = (double)imageBounds.Width / (double)drawableSize.Width; double ratioY = (double)imageBounds.Height / (double)drawableSize.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = drawableSize.Width * ratio; double newHeight = drawableSize.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; Drawable.Draw(canvas, new Rectangle(cx, cy, newWidth, newHeight), paint); } if (IsCheckable && IsSelected) { paint.Color = SelectionColor.ToSKColor(); paint.IsStroke = false; canvas.DrawOval(square.ToSKRect(), paint); double ratioX = (double)imageBounds.Width / (double)TickBitmap.Width; double ratioY = (double)imageBounds.Height / (double)TickBitmap.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = TickBitmap.Width * ratio; double newHeight = TickBitmap.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; canvas.DrawBitmap(TickBitmap, new SKRect(cx, cy, (float)(cx + newWidth), (float)(cy + newHeight)), paint); } } }
public static void RenderTexture(SKCanvas canvas, SKBitmap bitmap, SKRect rect, float opacity = 1f) { var color = new SKColor(255, 255, 255, (byte) (255*opacity)); var paint = new SKPaint {Color = color, FilterQuality = SKFilterQuality.High}; canvas.DrawBitmap(bitmap, rect, paint); }
public static void ChainedImageFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; var size = width > height ? height : width; var small = size / 5; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) using (var filterMag = SKImageFilter.CreateMagnifier(SKRect.Create(small*2, small*2, small*3, small*3), small)) using (var filterBlur = SKImageFilter.CreateBlur(5, 5, filterMag)) using (var paint = new SKPaint()) { paint.ImageFilter = filterBlur; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
public static void DilateImageFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) using (var filter = SKImageFilter.CreateDilate(5, 5)) using (var paint = new SKPaint()) { paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
public static void ColorTableColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; var ct = new byte[256]; for (int i = 0; i < 256; ++i) { var x = (i - 96) * 255 / 64; ct[i] = x < 0 ? (byte)0 : x > 255 ? (byte)255 : (byte)x; } // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) using (var cf = SKColorFilter.CreateTable(null, ct, ct, ct)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
public static void ColorMatrixColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) { var f = new Action<SKRect, float[]>((rect, colorMatrix) => { using (var cf = SKColorFilter.CreateColorMatrix(colorMatrix)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, rect, paint); } }); var colorMatrix1 = new float[20] { 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var grayscale = new float[20] { 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix3 = new float[20] { -1f, 1f, 1f, 0f, 0f, 1f, -1f, 1f, 0f, 0f, 1f, 1f, -1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var colorMatrix4 = new float[20] { 0.0f, 0.5f, 0.5f, 0f, 0f, 0.5f, 0.0f, 0.5f, 0f, 0f, 0.5f, 0.5f, 0.0f, 0f, 0f, 0.0f, 0.0f, 0.0f, 1f, 0f }; var highContrast = new float[20] { 4.0f, 0.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 4.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 4.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix6 = new float[20] { 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var sepia = new float[20] { 0.393f, 0.769f, 0.189f, 0.0f, 0.0f, 0.349f, 0.686f, 0.168f, 0.0f, 0.0f, 0.272f, 0.534f, 0.131f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var inverter = new float[20] { -1f, 0f, 0f, 0f, 255f, 0f, -1f, 0f, 0f, 255f, 0f, 0f, -1f, 0f, 255f, 0f, 0f, 0f, 1f, 0f }; var matices = new[] { colorMatrix1, grayscale, highContrast, sepia, colorMatrix3, colorMatrix4, colorMatrix6, inverter }; var cols = width < height ? 2 : 4; var rows = (matices.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { f(SKRect.Create(x * w, y * h, w, h), matices[y * cols + x]); } } } }
public static void BitmapDecoder(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; canvas.Clear(SKColors.White); // load the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var decoder = new SKImageDecoder(stream)) using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Arial")) { paint.IsAntialias = true; paint.TextSize = 14; paint.Typeface = tf; paint.Color = SKColors.Black; // read / set decoder settings decoder.DitherImage = true; decoder.PreferQualityOverSpeed = true; decoder.SampleSize = 2; // decode the image using (var bitmap = new SKBitmap()) { var result = decoder.Decode(stream, bitmap); if (result != SKImageDecoderResult.Failure) { var info = bitmap.Info; var x = 25; var y = 25; canvas.DrawBitmap(bitmap, x, y); x += info.Width + 25; y += 14; canvas.DrawText(string.Format("Result: {0}", result), x, y, paint); y += 20; canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint); y += 20; canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint); } } } }
public static void BitmapDecoder(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; canvas.Clear(SKColors.White); // load the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var codec = SKCodec.Create(stream)) using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Arial")) { var info = codec.Info; paint.IsAntialias = true; paint.TextSize = 14; paint.Typeface = tf; paint.Color = SKColors.Black; // decode the image using (var bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul)) { IntPtr length; var result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out length)); if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput) { var x = 25; var y = 25; canvas.DrawBitmap(bitmap, x, y); x += bitmap.Info.Width + 25; y += 14; canvas.DrawText(string.Format("Result: {0}", result), x, y, paint); y += 20; canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint); y += 20; canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint); } } } }