private void DrawSquare(CanvasControl sender, CanvasDrawingSession ds) { var width = (float) sender.ActualWidth; var height = (float) sender.ActualHeight; var stroke = this.defaultStroke; var min = Math.Min(width, height); min -= stroke * 2; var rect = new Rect(stroke, stroke, min, min); ds.FillRectangle(rect, ForegroundColor); ds.DrawRectangle(rect, GlowColor, stroke); }
private void DrawSelectionBoundingRect(CanvasDrawingSession ds) { if (selectionBoundingRect == null || selectionBoundingRect.Value.Width == 0 || selectionBoundingRect.Value.Height == 0 || selectionBoundingRect.Value.IsEmpty) { return; } ds.DrawRectangle(selectionBoundingRect.Value, Colors.Magenta); }
double DrawSourceImage(CanvasDrawingSession ds, Rect sourceRect, float width) { var sourceImageOffsetX = (float)((width / 2) - (sourceBitmap.Size.Width / 2)); var sourceImageOffsetY = (float)(10); ds.DrawImage(sourceBitmap, sourceImageOffsetX, sourceImageOffsetY); sourceRect.X += sourceImageOffsetX; sourceRect.Y += sourceImageOffsetY; ds.DrawRectangle(sourceRect, Colors.Yellow, 1, hairline); return sourceImageOffsetY + sourceBitmap.Size.Height * 1.2; }
private void DrawGrid(CanvasDrawingSession ds, Vector2 cellAcross, Vector2 cellDown, Vector2 topLeft) { var gridTopLeft = topLeft; var gridBottomRight = topLeft + cellAcross * columns + cellDown * rows; ds.DrawRectangle(new Rect(gridTopLeft.ToPoint(), gridBottomRight.ToPoint()), Colors.Black, 1); for (int row = 0; row <= rows; ++row) { ds.DrawLine(topLeft + cellDown * row, topLeft + cellAcross * columns + cellDown * row, Colors.Black, 1); } for (int column = 0; column <= columns; ++column) { ds.DrawLine(topLeft + cellAcross * column, topLeft + cellAcross * column + cellDown * rows, Colors.Black, 1); } }
public void Draw(CanvasDrawingSession ds, int frameCounter, float width, float height) { var sz = sourceBitmap.Size; Rect sourceRect = new Rect( sz.Width * 0.25 + Math.Sin(frameCounter * 0.02) * (sz.Width * 0.5), sz.Height * 0.25 + Math.Cos(frameCounter * 0.01) * (sz.Height * 0.5), sz.Width * 0.5, sz.Height * 0.5); double y = DrawSourceImage(ds, sourceRect, width); double displayWidth = width / 2; double x = displayWidth; double destHeight = (height - y) / 3; Rect bitmapDestRect = new Rect(x, y + 5, displayWidth, destHeight - 10); y += destHeight; Rect bitmapDestRect2 = new Rect(x, y + 5, displayWidth, destHeight - 10); y += destHeight; Rect effectDestRect = new Rect(x, y + 5, displayWidth, destHeight - 10); var format = new CanvasTextFormat() { FontSize = 14, HorizontalAlignment = CanvasHorizontalAlignment.Right, VerticalAlignment = CanvasVerticalAlignment.Center }; ds.DrawText("D2D DrawBitmap", 0, (float)bitmapDestRect.Y, (float)displayWidth - 10, (float)destHeight, Colors.White, format); ds.DrawText("D2D DrawImage (bitmap)", 0, (float)bitmapDestRect2.Y, (float)displayWidth - 10, (float)destHeight, Colors.White, format); ds.DrawText("D2D DrawImage (effect)", 0, (float)effectDestRect.Y, (float)displayWidth - 10, (float)destHeight, Colors.White, format); ds.FillRectangle(bitmapDestRect, fillPattern); ds.FillRectangle(bitmapDestRect2, fillPattern); ds.FillRectangle(effectDestRect, fillPattern); ds.DrawImage(sourceBitmap, bitmapDestRect, sourceRect); ds.DrawImage(sourceBitmap, bitmapDestRect2, sourceRect, 1, CanvasImageInterpolation.Cubic); ds.DrawImage(sourceEffect, effectDestRect, sourceRect); ds.DrawRectangle(bitmapDestRect, Colors.Yellow, 1, hairline); ds.DrawRectangle(bitmapDestRect2, Colors.Yellow, 1, hairline); ds.DrawRectangle(effectDestRect, Colors.Yellow, 1, hairline); }
private void DrawRectangle(CanvasControl sender, CanvasDrawingSession ds) { var width = (float)sender.ActualWidth; var height = (float)sender.ActualHeight; int steps = Math.Min((int)(width / 10), 20); for (int i = 0; i < steps; ++i) { var mu = (float)i / steps; var color = GradientColor(mu); mu *= 0.5f; var x = mu * width; var y = mu * height; var xx = (1 - mu) * width; var yy = (1 - mu) * height; ds.DrawRectangle(x, y, xx - x, yy - y, color, 2.0f); } }
private static void DrawRectangleInternal( CanvasDrawingSession ds, Color brush, Color pen, CanvasStrokeStyle ss, bool isStroked, bool isFilled, ref Rect2 rect, double strokeWidth) { if (isFilled) { ds.FillRectangle( (float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height, brush); } if (isStroked) { ds.DrawRectangle( (float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height, pen, (float)strokeWidth, ss); } }
void DrawSourceGraphic(PerDeviceResources resources, CanvasDrawingSession ds, float offset) { var source = GetSourceBitmap(resources); if (source != null) { // We can either draw a precreated bitmap... ds.DrawImage(source, offset, offset); } else { // ... or directly draw some shapes. ds.FillRectangle(offset, offset, testSize, testSize, Colors.Gray); ds.DrawLine(offset, offset, offset + testSize, offset + testSize, Colors.Red); ds.DrawLine(offset + testSize, offset, offset, offset + testSize, Colors.Red); ds.DrawRectangle(offset + 0.5f, offset + 0.5f, testSize - 1, testSize - 1, Colors.Blue); ds.DrawText("DPI test", new Vector2(offset + testSize / 2), Colors.Blue, resources.TextFormat); resources.AddMessage("DrawingSession ->\n"); } }
void DrawStuff(CanvasDrawingSession ds) { int horizontalLimit = (int)m_canvasControl.ActualWidth; int verticalLimit = (int)m_canvasControl.ActualHeight; const float thickStrokeWidth = 80.0f; DrawnContentType drawnContentType = (DrawnContentType)m_drawnContentTypeCombo.SelectedValue; ds.Clear(NextRandomColor()); Rect rect; Vector2 point; float radiusX; float radiusY; switch (drawnContentType) { case DrawnContentType.Clear_Only: break; case DrawnContentType.Bitmap: if (m_bitmap_tiger != null) { ds.DrawImage(m_bitmap_tiger, NextRandomPoint(horizontalLimit, verticalLimit).ToVector2()); } else { DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2); } break; case DrawnContentType.Effect_Blur: if (m_bitmap_tiger != null) { GaussianBlurEffect blurEffect = new GaussianBlurEffect(); blurEffect.StandardDeviation = 2.0f; blurEffect.Source = m_bitmap_tiger; ds.DrawImage(blurEffect, NextRandomPoint(horizontalLimit, verticalLimit).ToVector2()); } else { DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2); } break; case DrawnContentType.Line_Thin: ds.DrawLine( NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(), NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(), NextRandomColor()); break; case DrawnContentType.Line_Thick: ds.DrawLine( NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(), NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(), NextRandomColor(), thickStrokeWidth); break; case DrawnContentType.Rectangle_Thin: ds.DrawRectangle( NextRandomRect(horizontalLimit, verticalLimit), NextRandomColor()); break; case DrawnContentType.Rectangle_Thick: ds.DrawRectangle( NextRandomRect(horizontalLimit, verticalLimit), NextRandomColor(), thickStrokeWidth); break; case DrawnContentType.Rectangle_Filled: ds.FillRectangle( NextRandomRect(horizontalLimit, verticalLimit), NextRandomColor()); break; case DrawnContentType.RoundedRectangle_Thin: NextRandomRoundedRect(horizontalLimit, verticalLimit, out rect, out radiusX, out radiusY); ds.DrawRoundedRectangle( rect, radiusX, radiusY, NextRandomColor()); break; case DrawnContentType.RoundedRectangle_Thick: NextRandomRoundedRect(horizontalLimit, verticalLimit, out rect, out radiusX, out radiusY); ds.DrawRoundedRectangle( rect, radiusX, radiusY, NextRandomColor(), thickStrokeWidth); break; case DrawnContentType.Ellipse_Thin: NextRandomEllipse(horizontalLimit, verticalLimit, out point, out radiusX, out radiusY); ds.DrawEllipse( point, radiusX, radiusY, NextRandomColor()); break; case DrawnContentType.Ellipse_Thick: NextRandomEllipse(horizontalLimit, verticalLimit, out point, out radiusX, out radiusY); ds.DrawEllipse( point, radiusX, radiusY, NextRandomColor(), thickStrokeWidth); break; case DrawnContentType.Ellipse_Fill: NextRandomEllipse(horizontalLimit, verticalLimit, out point, out radiusX, out radiusY); ds.FillEllipse( point, radiusX, radiusY, NextRandomColor()); break; case DrawnContentType.Circle_Fill: ds.FillCircle( NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(), 100, NextRandomColor()); break; case DrawnContentType.Dashed_Lines: DrawDashedLines(ds, NextRandomColor(), horizontalLimit, verticalLimit); break; case DrawnContentType.Text: var p = NextRandomPoint(horizontalLimit, verticalLimit); var x = (float)p.X; var y = (float)p.Y; var color = NextRandomColor(); ds.DrawLine(new Vector2(x, 0), new Vector2(x, verticalLimit), color); ds.DrawLine(new Vector2(0, y), new Vector2(horizontalLimit, y), color); ds.DrawText( "Centered", p.ToVector2(), color, new CanvasTextFormat() { FontSize = 18, VerticalAlignment = CanvasVerticalAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }); var r = NextRandomRect(horizontalLimit, verticalLimit); ds.DrawRectangle(r, color); ds.DrawText( m_quiteLongText, r, NextRandomColor(), new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 18, ParagraphAlignment = ParagraphAlignment.Justify, Options = CanvasDrawTextOptions.Clip }); break; case DrawnContentType.ImageBrush: if (m_bitmap_tiger != null) { m_imageBrush.Image = m_bitmap_tiger; m_imageBrush.ExtendX = (CanvasEdgeBehavior)(m_random.Next(3)); m_imageBrush.ExtendY = (CanvasEdgeBehavior)(m_random.Next(3)); ds.FillRectangle(new Rect(0, 0, horizontalLimit, verticalLimit), m_imageBrush); } else DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2); break; case DrawnContentType.Test_Scene0_Default: GeometryTestScene0.DrawGeometryTestScene(ds, TestSceneRenderingType.Default); break; case DrawnContentType.Test_Scene0_Wireframe: GeometryTestScene0.DrawGeometryTestScene(ds, TestSceneRenderingType.Wireframe); break; case DrawnContentType.Test_Scene1_Default: GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Default); break; case DrawnContentType.Test_Scene1_Randomized: GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Randomized); break; case DrawnContentType.Test_Scene1_Wireframe: GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Wireframe); break; default: System.Diagnostics.Debug.Assert(false); // Unexpected break; } }