private Texture2D getOrCreateTexture(GraphicsDevice device) { if (this.bitmap is null || this.texture is null) { this.bitmap?.Dispose(); this.texture?.Dispose(); this.bitmap = new SKBitmap(this.mBounds.Width, this.mBounds.Height); using var ms = new MemoryStream(); using var canvas = new SKCanvas(this.bitmap); using var white_skpaint = new SKPaint() { Shader = SKShader.CreateLinearGradient(new SKPoint(0.0f, 0.0f), new SKPoint(this.mBounds.Width, 0.0f), new SKColor[] { SKColors.Transparent, SKColors.White }, SKShaderTileMode.Decal) }; using var black_skpaint = new SKPaint() { Shader = SKShader.CreateLinearGradient(new SKPoint(0.0f, 0.0f), new SKPoint(this.mBounds.Height, 0.0f), new SKColor[] { SKColors.Transparent, new SKColor(1, 1, 1, 255) }, SKShaderTileMode.Decal) }; using var colour_skpaint = new SKPaint() { Shader = SKShader.CreateColor(this.hueColour.AsSKColor()) }; canvas.DrawPaint(colour_skpaint); canvas.DrawPaint(white_skpaint); canvas.RotateDegrees(90.0f); canvas.DrawPaint(black_skpaint); this.bitmap.Encode(ms, SKEncodedImageFormat.Png, 95); this.texture = Texture2D.FromStream(device, ms); } return(this.texture); }
public void Draw(SKCanvas canvas, SKRect rect) { canvas.Save(); canvas.Scale(rect.Height / 100f); canvas.DrawPaint(backgroundPaint); canvas.Restore(); // drawing horizontal lines for (int i = 0; i < instruments.Length; ++i) { float line = gridRect.Top + (i + 1) * settings.LineHeight; canvas.DrawText(instruments[i].Name, 0, line, textPaint); DrawHorizontalLine(canvas, line); } int col = (int)gridRect.Left; float step = settings.BeatWidth / (float)subdivisions; for (int bar = 0; bar < info.BarsCount; ++bar) { for (int beatIndex = 1; beatIndex <= info.BeatsPerBar; ++beatIndex) { DrawVerticalLine(canvas, col, beatIndex); for (var subDivIndex = 1; subDivIndex < subdivisions; ++subDivIndex) { DrawVerticalLine(canvas, col + subDivIndex * step, (int?)null); } col += settings.BeatWidth; } } }
private void PrepareCanvas(SKCanvas canvas, SKImageInfo info) { canvas.Clear(); canvas.DrawPaint(new SKPaint { Color = new SKColor(255, 255, 255) }); }
void DisplayClipOp(SKCanvas canvas, SKRect rect, SKClipOperation clipOp) { float textSize = textPaint.TextSize; canvas.DrawText(clipOp.ToString(), rect.MidX, rect.Top + textSize, textPaint); rect.Top += textSize; float radius = 0.9f * Math.Min(rect.Width / 3, rect.Height / 2); float xCenter = rect.MidX; float yCenter = rect.MidY; canvas.Save(); using (SKPath path1 = new SKPath()) { path1.AddCircle(xCenter - radius / 2, yCenter, radius); canvas.ClipPath(path1); using (SKPath path2 = new SKPath()) { path2.AddCircle(xCenter + radius / 2, yCenter, radius); canvas.ClipPath(path2, clipOp); canvas.DrawPaint(fillPaint); } } canvas.Restore(); }
private void DrawTimer(SKCanvas canvas, SKImageInfo info) { canvas.Clear(); canvas.DrawPaint(new SKPaint { Color = new SKColor(96, 96, 96) }); SKPoint center = new SKPoint(info.Width / 2 - 15, info.Height / 2); float radius = Math.Min(info.Width / 2, info.Height / 2) - 10; SKRect rect = new SKRect(center.X - radius, center.Y - radius, center.X + radius, center.Y + radius); var surroundCircle = new YCircle(center, radius + 2, new SKColor(48, 48, 48), 4, SKPaintStyle.Stroke); surroundCircle.Draw(canvas); var backgroundCircle = new YCircle(center, radius, new SKColor(80, 80, 80), 1, SKPaintStyle.Fill); backgroundCircle.Draw(canvas); float startAngle = 270; var path = new YPath(new SKPath(), new SKColor(245, 222, 179), 5); path.DrawingPath.MoveTo(center); path.Paint.Style = SKPaintStyle.Fill; path.DrawingPath.ArcTo(rect, startAngle, _sweepAngle, false); path.DrawingPath.Close(); path.Draw(canvas); }
public async ValueTask <byte[]> GetBytesWithFilterAsync( int size, string topic = null, SKImageFilter imageFilter = null, SKColorFilter colorFilter = null, SKShader shader = null, SKBlendMode blendMode = SKBlendMode.Overlay) { Trace.WriteLine("Start"); string url; if (string.IsNullOrEmpty(topic)) { url = string.Format(URL_PATTERN_RND, size); } else { url = string.Format(URL_PATTERN, size, topic); } var imageInfo = new SKImageInfo(size, size); using (var http = new HttpClient()) { var image = await http.GetByteArrayAsync(url);//.ConfigureAwait(false); Trace.WriteLine("Downloaded"); using (var outStream = new MemoryStream()) using (var bitmap = SKBitmap.Decode(image, imageInfo)) using (var surface = SKSurface.Create(imageInfo)) using (var paint = new SKPaint()) { SKCanvas canvas = surface.Canvas; canvas.DrawColor(SKColors.White); if (imageFilter != null) { paint.ImageFilter = imageFilter; } if (colorFilter != null) { paint.ColorFilter = colorFilter; } // draw the bitmap through the filter var rect = SKRect.Create(imageInfo.Size); canvas.DrawBitmap(bitmap, rect, paint); if (shader != null) { paint.Shader = shader; paint.BlendMode = blendMode; canvas.DrawPaint(paint); } SKData data = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80); data.SaveTo(outStream); byte[] manipedImage = outStream.ToArray(); Trace.WriteLine("End"); return(manipedImage); } } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); // Set transform to center and enlarge clip path to window height SKRect bounds; path.GetTightBounds(out bounds); canvas.Translate(info.Width / 2, info.Height / 2); canvas.Scale((0.5f * info.Height / bounds.Height) * -1); canvas.Translate(-bounds.MidX, -bounds.MidY); // Set the clip path canvas.ClipPath(path, SKClipOperation.Difference); // Reset transforms canvas.ResetMatrix(); using (SKPaint paint2 = new SKPaint()) { paint2.Style = SKPaintStyle.Fill; paint2.Color = SKColors.Green.WithAlpha(0x50); canvas.DrawPaint(paint2); } }
private Texture2D getOrCreateTexture(GraphicsDevice device) { if (this.bitmap is null || this.texture is null) { this.bitmap?.Dispose(); this.texture?.Dispose(); this.bitmap = new SKBitmap(this.mBounds.Width, this.mBounds.Height); using var ms = new MemoryStream(); using var canvas = new SKCanvas(this.bitmap); using var paint = new SKPaint() { Shader = SKShader.CreateLinearGradient(new SKPoint(0.0f, 0.0f), new SKPoint(this.mBounds.Width, this.mBounds.Height), new SKColor[] { SKColors.Red, SKColors.Yellow, SKColors.Lime, SKColors.Cyan, SKColors.Blue, SKColors.Magenta, SKColors.Red }, new Single[] { 0f, 1 / 6f, 2 / 6f, 3 / 6f, 4 / 6f, 5 / 6f, 1f }, SKShaderTileMode.Decal) }; canvas.DrawPaint(paint); canvas.RotateDegrees(90.0f); this.bitmap.Encode(ms, SKEncodedImageFormat.Png, 95); this.texture = Texture2D.FromStream(device, ms); } return(this.texture); }
private void canvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) { SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); //Set transforms to shift path to center and scale to canvas size canvas.Translate(info.Width / 2, info.Height / 2); canvas.Scale(0.95f * Math.Min(info.Width / (pathBounds.Width + STROKE_WIDTH), info.Height / (pathBounds.Height + STROKE_WIDTH))); using (SKPaint paint = new SKPaint()) { paint.Style = SKPaintStyle.Stroke; paint.StrokeWidth = STROKE_WIDTH; paint.Shader = SKShader.CreateLinearGradient( new SKPoint(pathBounds.Left, pathBounds.Top), new SKPoint(pathBounds.Right, pathBounds.Bottom), colors, null, SKShaderTileMode.Repeat, SKMatrix.MakeTranslation(offset, 0)); canvas.DrawPaint(paint); } }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { // load the image from the embedded resource stream using (var stream = new SKManagedStream(SampleMedia.Images.ColorWheel)) using (var source = SKBitmap.Decode(stream)) { // invert the pixels var pixels = source.Pixels; for (int i = 0; i < pixels.Length; i++) { pixels[i] = new SKColor( (byte)(255 - pixels[i].Red), (byte)(255 - pixels[i].Green), (byte)(255 - pixels[i].Blue), pixels[i].Alpha); } source.Pixels = pixels; using (var shader = SKShader.CreateBitmap(source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat)) using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear(SKColors.White); canvas.DrawPaint(paint); } } }
public BlueBananaPage() { Title = "Blue Banana"; // Load banana matte bitmap (black on transparent) SKBitmap matteBitmap = BitmapExtensions.LoadBitmapResource( typeof(BlueBananaPage), "SkiaSharpFormsDemos.Media.BananaMatte.png"); // Create a bitmap with a solid blue banana and transparent otherwise blueBananaBitmap = new SKBitmap(matteBitmap.Width, matteBitmap.Height); using (SKCanvas canvas = new SKCanvas(blueBananaBitmap)) { canvas.Clear(); canvas.DrawBitmap(matteBitmap, new SKPoint(0, 0)); using (SKPaint paint = new SKPaint()) { paint.Color = SKColors.Blue; paint.BlendMode = SKBlendMode.SrcIn; canvas.DrawPaint(paint); } } SKCanvasView canvasView = new SKCanvasView(); canvasView.PaintSurface += OnCanvasViewPaintSurface; Content = canvasView; }
/// <summary> /// </summary> /// <param name="canvas"></param> /// <param name="width"></param> /// <param name="height"></param> public void DrawGraph(SKCanvas canvas, int width, int height) { UpdateReferences(width, height); SetScale(width, height); using (var paint = new SKPaint()) { paint.IsAntialias = true; var colors = new[] { SKColors.White, new SKColor(214, 244, 255) }; paint.Shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(width, height), colors, null, SKShaderTileMode.Clamp ); canvas.DrawPaint(paint); paint.StrokeCap = SKStrokeCap.Round; paint.IsAntialias = true; paint.StrokeJoin = SKStrokeJoin.Round; canvas.DrawPaint(paint); } DrawSeparators(canvas); for (var i = 1; i < _graph.Count; i++) { DrawLine(canvas, width, height, i, _graph[i - 1], _graph[i]); } for (var i = 0; i < _graph.Count; i++) { var x = _paddingW + _availableW * i / (_sizeX - 1); var y = height - (_paddingH * 2f + 0.8f * _availableH * (_graph[i].Y - _minY) / _sizeY); DrawWeatherImag(canvas, _graph[i], x, y, _scale); DrawYVal(canvas, _graph[i], x, y, _scale); DrawLabel(canvas, _graph[i], x, y, _scale); } }
public static void DrawCheckerboard(SKCanvas canvas, SKColor c1, SKColor c2, int size) { SKPaint paint = new SKPaint { Shader = CreateCheckerboardShader(c1, c2, size) }; canvas.DrawPaint(paint); }
public void Clear(ColorValue backgroundColor) { canvas.Clear(); using (var paint = new SKPaint()) { paint.Color = GetColorFromColorValue(backgroundColor); canvas.DrawPaint(paint); } }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); using (var shader = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0.0f)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
/// <summary> /// Draw on the canvas /// </summary> /// <param name="sender">object</param> /// <param name="e">SKPaintSurfaceEventArgs</param> private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e) { SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.DrawPaint(backgroundFillPaint); int width = e.Info.Width; int height = e.Info.Height; DateTime dateTime = DateTime.Now; canvas.Translate(width / 2, height / 2); canvas.Scale(Math.Min(width / 80f, height / 200f)); canvas.DrawCircle(0, -160, 75, blackFillPaint); canvas.DrawCircle(0, 0, 100, blackFillPaint); // Display the face of the watch for (int angle = 0; angle < 360; angle += 6) { if (angle % 30 == 0) { canvas.DrawCircle(0, -90, angle % 30 == 0 ? 4 : 2, redFillPaint); } else { canvas.DrawCircle(0, -90, angle % 30 == 0 ? 4 : 2, whiteFillPaint); } canvas.RotateDegrees(6); } // Hour hand canvas.Save(); canvas.RotateDegrees(30 * dateTime.Hour + dateTime.Minute / 2f); canvas.DrawPath(hourHandPath, grayFillPaint); canvas.DrawPath(hourHandPath, whiteStrokePaint); canvas.Restore(); // Minute hand canvas.Save(); canvas.RotateDegrees(6 * dateTime.Minute + dateTime.Second / 10f); canvas.DrawPath(minHandPath, grayFillPaint); canvas.DrawPath(minHandPath, whiteStrokePaint); canvas.Restore(); // Second hand canvas.Save(); float seconds = dateTime.Second + dateTime.Millisecond / 1000f; canvas.RotateDegrees(6 * seconds); canvas.DrawLine(0, 0, 0, -80, whiteStrokePaint); canvas.Restore(); }
public void PopOpacityMask() { Canvas.SaveLayer(new SKPaint { BlendMode = SKBlendMode.DstIn }); using (var paintWrapper = maskStack.Pop()) { Canvas.DrawPaint(paintWrapper.Paint); } Canvas.Restore(); Canvas.Restore(); }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { var colors = new[] { SKColors.Cyan, SKColors.Magenta, SKColors.Yellow, SKColors.Cyan }; var center = new SKPoint(width / 2f, height / 2f); using (var shader = SKShader.CreateSweepGradient(center, colors, null)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { var colors = new[] { SKColors.Blue, SKColors.Yellow }; var center = new SKPoint(width / 2f, height / 2f); using (var shader1 = SKShader.CreateRadialGradient(center, 180.0f, colors, null, SKShaderTileMode.Clamp)) using (var shader2 = SKShader.CreatePerlinNoiseTurbulence(0.025f, 0.025f, 2, 0.0f)) using (var shader = SKShader.CreateCompose(shader1, shader2)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
public override void Awake(NSObject context) { base.Awake(context); var scale = WKInterfaceDevice.CurrentDevice.ScreenScale; var bitmap = new SKBitmap((int)(ContentFrame.Width * scale), (int)(ContentFrame.Height * scale)); var colors = new[] { SKColors.Cyan, SKColors.Magenta, SKColors.Yellow, SKColors.Cyan }; var center = new SKPoint(bitmap.Width / 2, bitmap.Height / 2); using (var canvas = new SKCanvas(bitmap)) { canvas.Clear(SKColors.Transparent); using (var path = new SKPath()) { path.AddRoundedRect(new SKRect(5, 5, bitmap.Width - 5, bitmap.Height - 5), 5, 5); canvas.ClipPath(path); } using (var paint = new SKPaint()) using (var gradient = SKShader.CreateSweepGradient(center, colors, null)) { paint.IsAntialias = true; paint.Shader = gradient; canvas.DrawPaint(paint); paint.Shader = null; } using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("San Fransisco")) { paint.IsAntialias = true; paint.Color = SKColors.DarkBlue; paint.TextSize = (float)(20 * scale); paint.TextAlign = SKTextAlign.Center; paint.Typeface = tf; canvas.DrawText("SkiaSharp", center.X, (center.Y / 2) + (paint.TextSize / 2), paint); } } var image = bitmap.ToUIImage(scale, UIKit.UIImageOrientation.Up); imageView.SetImage(image); }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { var ltColor = SKColors.White; var dkColor = SKColors.Black; using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(0, height), new[] { ltColor, dkColor }, null, SKShaderTileMode.Clamp)) { paint.Shader = shader; canvas.DrawPaint(paint); } } // Center and Scale the Surface var scale = (width < height ? width : height) / (240f); canvas.Translate(width / 2f, height / 2f); canvas.Scale(scale, scale); canvas.Translate(-128, -128); using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateTwoPointConicalGradient( new SKPoint(115.2f, 102.4f), 25.6f, new SKPoint(102.4f, 102.4f), 128.0f, new[] { ltColor, dkColor }, null, SKShaderTileMode.Clamp )) { paint.Shader = shader; canvas.DrawOval(new SKRect(51.2f, 51.2f, 204.8f, 204.8f), paint); } } }
void DisplayClipOp(SKCanvas canvas, SKRect rect, SKRegionOperation regionOp) { float textSize = textPaint.TextSize; canvas.DrawText(regionOp.ToString(), rect.MidX, rect.Top + textSize, textPaint); rect.Top += textSize; float radius = 0.9f * Math.Min(rect.Width / 3, rect.Height / 2); float xCenter = rect.MidX; float yCenter = rect.MidY; SKRectI recti = new SKRectI((int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom); using (SKRegion wholeRectRegion = new SKRegion()) { wholeRectRegion.SetRect(recti); using (SKRegion region1 = new SKRegion(wholeRectRegion)) using (SKRegion region2 = new SKRegion(wholeRectRegion)) { using (SKPath path1 = new SKPath()) { path1.AddCircle(xCenter - radius / 2, yCenter, radius); region1.SetPath(path1); } using (SKPath path2 = new SKPath()) { path2.AddCircle(xCenter + radius / 2, yCenter, radius); region2.SetPath(path2); } region1.Op(region2, regionOp); canvas.Save(); canvas.ClipRegion(region1); canvas.DrawPaint(fillPaint); canvas.Restore(); } } }
private void PaintGrayRadialGradient(SKCanvas canvas, float canvasRadius) { SKPoint center = new SKPoint(canvasRadius, canvasRadius); var colors = new SKColor[] { SKColors.Gray, SKColors.Transparent }; var shader = SKShader.CreateRadialGradient(center, WheelHSRadius(canvasRadius), colors, null, SKShaderTileMode.Clamp); var paint = new SKPaint { IsAntialias = true, Shader = shader, Style = SKPaintStyle.Fill }; canvas.DrawPaint(paint); }
public static void UpdateBackgroundCanvas(SKCanvas canvas, int width, int height) { try { #if !(GTK || CLI) var colors = new SKColor[] { Theme.PrimaryColor.Color.ToSKColor(), Theme.SecondaryColor.Color.ToSKColor() }; var positions = new float[] { 0.0f, 1.0f }; var gradient = SKShader.CreateLinearGradient(new SKPoint(0, height / 2), new SKPoint(width, height / 2), colors, positions, SKShaderTileMode.Mirror); var paint = new SKPaint { Shader = gradient, IsAntialias = true }; canvas.DrawPaint(paint); #endif } catch (Exception ex) { Log.IgnoreException(ex); } }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { // load the image from the embedded resource stream using (var stream = new SKManagedStream(SampleMedia.Images.ColorWheel)) using (var source = SKBitmap.Decode(stream)) { var matrix = SKMatrix.MakeRotation(30.0f); // create the shader and paint using (var shader = SKShader.CreateBitmap(source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, matrix)) using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear(SKColors.White); canvas.DrawPaint(paint); } } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); float size = Math.Min(info.Width, info.Height); float radius = 0.4f * size; float offset = size / 2 - radius; // Translate to center canvas.Translate(info.Width / 2, info.Height / 2); using (SKPath path = new SKPath()) { path.AddCircle(-offset, -offset, radius); canvas.ClipPath(path, SKClipOperation.Intersect); path.Reset(); path.AddCircle(-offset, offset, radius); canvas.ClipPath(path, SKClipOperation.Intersect); path.Reset(); path.AddCircle(offset, -offset, radius); canvas.ClipPath(path, SKClipOperation.Intersect); path.Reset(); path.AddCircle(offset, offset, radius); canvas.ClipPath(path, SKClipOperation.Intersect); using (SKPaint paint = new SKPaint()) { paint.Style = SKPaintStyle.Fill; paint.Color = SKColors.Blue; canvas.DrawPaint(paint); } } }
private void DrawSended(SKCanvas canvas, SKSurface surface, SKImageInfo info) { using (var image = surface.Snapshot()) using (var data = image.Encode(SKEncodedImageFormat.Png, 80)) using (var stream = File.OpenWrite(System.IO.Path.Combine("drawSaved", "1.png"))) { data.SaveTo(stream); } var paint = new SKPaint { Style = SKPaintStyle.Stroke, Color = new SKColor(50, 50, 50), TextSize = 70, IsAntialias = true, }; canvas.Clear(); canvas.DrawPaint(new SKPaint { Color = new SKColor(255, 255, 255) }); canvas.DrawText("Dessin envoyé", info.Width / 3 - 30, info.Height / 2 - 40, paint); canvas.DrawText("En attente des autres joueurs", info.Width / 6 - 80, info.Height / 2 + 50, paint); }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height) { var bitmap = new SKBitmap(width, height); using (var canvas = new SKCanvas(bitmap)) { canvas.Clear(SKColors.Black); // determine sizes for each image that will composited into the final image var iSlice = Convert.ToInt32(width * 0.23475); int iTrans = Convert.ToInt32(height * .25); int iHeight = Convert.ToInt32(height * .70); var horizontalImagePadding = Convert.ToInt32(width * 0.0125); var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111); int imageIndex = 0; for (int i = 0; i < 4; i++) { SKCodecOrigin origin; using (var currentBitmap = SkiaEncoder.Decode(paths[imageIndex], false, out origin)) { // resize to the same aspect as the original int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height); using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType)) { currentBitmap.Resize(resizeBitmap, SKBitmapResizeMethod.Lanczos3); // determine how much to crop int ix = (int)Math.Abs((iWidth - iSlice) / 2); using (var image = SKImage.FromBitmap(resizeBitmap)) { // crop image using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight))) { // draw image onto canvas canvas.DrawImage(subset, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing); using (var croppedBitmap = SKBitmap.FromImage(subset)) { // create reflection of image below the drawn image using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType)) { // resize to half height croppedBitmap.Resize(reflectionBitmap, SKBitmapResizeMethod.Lanczos3); using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType)) { using (var flippedCanvas = new SKCanvas(flippedBitmap)) { // flip image vertically var matrix = SKMatrix.MakeScale(1, -1); matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height); flippedCanvas.SetMatrix(matrix); flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0); flippedCanvas.ResetMatrix(); // create gradient to make image appear as a reflection var remainingHeight = height - (iHeight + (2 * verticalSpacing)); flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight)); using (var gradient = new SKPaint()) { gradient.IsAntialias = true; gradient.BlendMode = SKBlendMode.SrcOver; gradient.Shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, remainingHeight), new[] { new SKColor(0, 0, 0, 128), new SKColor(0, 0, 0, 208), new SKColor(0, 0, 0, 240), new SKColor(0, 0, 0, 255) }, null, SKShaderTileMode.Clamp); flippedCanvas.DrawPaint(gradient); } // finally draw reflection onto canvas canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing)); } } } } } } } } imageIndex++; if (imageIndex >= paths.Length) { imageIndex = 0; } } } return(bitmap); }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { var modes = Enum.GetValues(typeof(SKXferMode)).Cast <SKXferMode>().ToArray(); var cols = width < height ? 3 : 5; var rows = (modes.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; var rect = SKRect.Create(w, h); var srcPoints = new[] { new SKPoint(0.0f, 0.0f), new SKPoint(w, 0.0f) }; var srcColors = new[] { SKColors.Magenta.WithAlpha(0), SKColors.Magenta }; var dstPoints = new[] { new SKPoint(0.0f, 0.0f), new SKPoint(0.0f, h) }; var dstColors = new[] { SKColors.Cyan.WithAlpha(0), SKColors.Cyan }; using (var text = new SKPaint()) using (var stroke = new SKPaint()) using (var src = new SKPaint()) using (var dst = new SKPaint()) using (var srcShader = SKShader.CreateLinearGradient(srcPoints[0], srcPoints[1], srcColors, null, SKShaderTileMode.Clamp)) using (var dstShader = SKShader.CreateLinearGradient(dstPoints[0], dstPoints[1], dstColors, null, SKShaderTileMode.Clamp)) { text.TextSize = 12.0f; text.IsAntialias = true; text.TextAlign = SKTextAlign.Center; stroke.IsStroke = true; src.Shader = srcShader; dst.Shader = dstShader; canvas.Clear(SKColors.White); for (var i = 0; i < modes.Length; ++i) { using (new SKAutoCanvasRestore(canvas, true)) { canvas.Translate(w * (i / rows), h * (i % rows)); canvas.ClipRect(rect); canvas.DrawColor(SKColors.LightGray); canvas.SaveLayer(null); canvas.Clear(SKColors.Transparent); canvas.DrawPaint(dst); src.XferMode = modes[i]; canvas.DrawPaint(src); canvas.DrawRect(rect, stroke); var desc = modes[i].ToString(); canvas.DrawText(desc, w / 2f, h / 2f, text); } } } }
public static void Draw() { const int beatsPerBar = 4; const int nbBars = 2; const int unitPerBeat = 2; var allBeats = nbBars * beatsPerBar * unitPerBeat; const int blockHeight = 24; const int blockWidth = 32; //const int heightInBlocks = 8; const int separator = 1; string[] instru = new string[] { "HH", "SN", "T1", "CR", "RD" }; var upColor = new SKColor(25, 146, 191); var downColor = new SKColor(37, 92, 123); var gradient = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, 200), new[] { upColor, downColor }, null, SKShaderTileMode.Mirror); using (var headerPaint = new SKPaint()) { headerPaint.Color = SKColors.White; headerPaint.IsAntialias = true; headerPaint.FakeBoldText = false; headerPaint.IsDither = false; headerPaint.StrokeWidth = 1; headerPaint.Style = SKPaintStyle.StrokeAndFill; headerPaint.Typeface = SKTypeface.FromFamilyName("Arial Black", SKTypefaceStyle.Normal); headerPaint.LcdRenderText = true; headerPaint.TextAlign = SKTextAlign.Left; headerPaint.TextSize = 24; var headerWidth = instru.Max(i => headerPaint.MeasureText(i)) + 4; using (var surface = SKSurface.Create((allBeats + 1) * blockWidth + (int)Math.Ceiling(headerWidth), (instru.Length + 2) * (blockHeight + separator), SKImageInfo.PlatformColorType, SKAlphaType.Premul)) { SKCanvas myCanvas = surface.Canvas; using (var backgroundPaint = new SKPaint()) { backgroundPaint.Shader = gradient; myCanvas.DrawPaint(backgroundPaint); } var gridTop = blockHeight + separator; var gridBottom = (blockHeight + separator) * (instru.Length) + gridTop; var gridLeft = headerWidth; var gridRight = gridLeft + allBeats * blockWidth; var height = gridTop - 1; var fadeOut = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(headerWidth, 0), new[] { new SKColor(255, 255, 255, 0), SKColors.White }, null, SKShaderTileMode.Clamp); myCanvas.DrawLine(gridLeft, height + 1, gridRight, height + 1, headerPaint); headerPaint.Shader = fadeOut; myCanvas.DrawLine(0, height + 1, headerWidth, height + 1, headerPaint); headerPaint.Shader = null; height += blockHeight + separator; for (var index = 0; index < instru.Length; ++index) { myCanvas.DrawText(instru[index], 0, height - 2, headerPaint); myCanvas.DrawLine(gridLeft, height + 1, gridRight, height + 1, headerPaint); headerPaint.Shader = fadeOut; myCanvas.DrawLine(0, height + 1, headerWidth, height + 1, headerPaint); headerPaint.Shader = null; height += blockHeight + separator; } myCanvas.DrawLine(headerWidth, gridTop, headerWidth, gridBottom, headerPaint); var x = headerWidth + blockWidth; for (var beatIndex = 1; beatIndex <= allBeats; ++beatIndex) { myCanvas.DrawLine(x, gridTop, x, gridBottom, headerPaint); if (beatIndex % unitPerBeat == 1) { var fullBeatIndex = ((beatIndex - 1) / unitPerBeat) % beatsPerBar + 1; var numberSize = headerPaint.MeasureText(fullBeatIndex.ToString()); var left = x - blockWidth / 2 - numberSize / 2; myCanvas.DrawText(fullBeatIndex.ToString(), left, gridTop - 4, headerPaint); } x += blockWidth; } // Your drawing code goes here. using (var img = surface.Snapshot()) { using (var data = img.Encode(SKEncodedImageFormat.Png, 80)) { // save the data to a stream using (var stream = System.IO.File.OpenWrite(@"C:\Temp\img.png")) { data.SaveTo(stream); } } } } } }