using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.Text; using Microsoft.Graphics.Canvas.UI.Xaml; public void DrawText(CanvasDrawingSession session, string text, float x, float y) { CanvasTextLayout layout = new CanvasTextLayout(session, text, new CanvasTextFormat(), 0, 0); session.DrawTextLayout(layout, x, y, Colors.Black); }
using Microsoft.Graphics.Canvas.UI.Xaml; public void DrawLine(CanvasDrawingSession session, float x1, float y1, float x2, float y2) { session.DrawLine(x1, y1, x2, y2, Colors.Blue, 2); }
using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.Brushes; using Microsoft.Graphics.Canvas.Geometry; using Microsoft.Graphics.Canvas.UI.Xaml; public void DrawRectangle(CanvasDrawingSession session, float x, float y, float width, float height) { CanvasGeometry rectangle = CanvasGeometry.CreateRectangle(session, x, y, width, height); CanvasSolidColorBrush brush = new CanvasSolidColorBrush(session, Colors.Red); session.FillGeometry(rectangle, brush); }This method draws a rectangle with the given dimensions and a red color. It uses CanvasGeometry to create the rectangle and CanvasSolidColorBrush to create the fill brush. It then uses CanvasDrawingSession's FillGeometry method to fill the rectangle. The package library used in these examples is the Win2D library, which provides a high-performance, immediate-mode 2D graphics API for UWP applications.