public static void RenderTexture(SKCanvas canvas, SKBitmap bitmap, float x, float y, float orientation = 0, float offsetX = 0, float offsetY = 0, LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center, LabelStyle.VerticalAlignmentEnum verticalAlignment = LabelStyle.VerticalAlignmentEnum.Center, float opacity = 1f, float scale = 1f) { canvas.Save(); canvas.Translate(x, y); canvas.RotateDegrees(orientation, 0, 0); // todo: or degrees? canvas.Scale(scale, scale); x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, bitmap.Width); y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, bitmap.Height); var halfWidth = bitmap.Width/2; var halfHeight = bitmap.Height/2; var rect = new SKRect(x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight); RenderTexture(canvas, bitmap, rect, opacity); canvas.Restore(); }
// ExStart:RenderShapeToGraphics public static string RenderShapeToGraphics(string dataDir, Shape shape) { ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // And make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25))) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // The shape that is rendered. In our case we will rotate the rendered shape. using (SkiaSharp.SKCanvas gr = new SkiaSharp.SKCanvas(bitmap)) { // Clear the shape with the background color of the document. gr.DrawColor(new SkiaSharp.SKColor(shape.Document.PageColor.R, shape.Document.PageColor.G, shape.Document.PageColor.B, shape.Document.PageColor.A)); // Center the rotation using translation method below gr.Translate((float)bitmap.Width / 8, (float)bitmap.Height / 2); // Rotate the image by 45 degrees. gr.RotateDegrees(45); // Undo the translation. gr.Translate(-(float)bitmap.Width / 8, -(float)bitmap.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } // Save output to file. using (System.IO.FileStream fs = System.IO.File.Create(dataDir + "/RenderToSize_Out.png")) { SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100); d.SaveTo(fs); } } return("\nShape rendered to graphics successfully.\nFile saved at " + dataDir); }
public void RotateDegrees(float degrees, float x, float y) => _canvas.RotateDegrees(degrees, x, y);
public static void ManageDrawMatrix (SKCanvas canvas, int width, int height) { var size = ((float)height > width ? width : height) * 0.5f; var center = new SKPoint ((width - size) / 2f, (height - size) / 2f); // draw these at specific locations var leftRect = SKRect.Create (center.X - size / 2f, center.Y, size, size); var rightRect = SKRect.Create (center.X + size / 2f, center.Y, size, size); // draw this at the current location / transformation var rotatedRect = SKRect.Create (0f, 0f, size, size); using (var paint = new SKPaint ()) { paint.IsAntialias = true; canvas.Clear (XamPurple); // draw paint.Color = XamDkBlue; canvas.DrawRect (leftRect, paint); // save canvas.Save(); // transform canvas.Translate (width / 2f, center.Y); canvas.RotateDegrees (45); // draw paint.Color = XamGreen; canvas.DrawRect (rotatedRect, paint); // undo transform / restore canvas.Restore(); // draw paint.Color = XamLtBlue; canvas.DrawRect (rightRect, paint); } }