6

I have a custom WPF Canvas, upon which I would like to show a grid. I do so by overriding the OnRender method on Canvas, and using the DrawingContext drawing functions. IsGridVisible, GridWidth, GridHeight are the number of pixels between each grid line horizontally and vertically respectively.

I also use a ScaleTransform on the Canvas.LayoutTransform property to zoom the Canvas items and as one expects, the grid line thicknesses are multiplied by the ScaleTransform scaling factors as shown in the below image. Is there any way to draw single pixel lines, irrespective of the current Canvas RenderTransform?

 protected override void OnRender(System.Windows.Media.DrawingContext dc) { base.OnRender(dc); if (IsGridVisible) { // Draw GridLines Pen pen = new Pen(new SolidColorBrush(GridColour), 1); pen.DashStyle = DashStyles.Dash; for (double x = 0; x < this.ActualWidth; x += this.GridWidth) { dc.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight)); } for (double y = 0; y < this.ActualHeight; y += this.GridHeight) { dc.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y)); } } } 

alt text http://www.freeimagehosting.net/uploads/f05ad1f602.png

3
  • did you try to set Pen thickness to 1.0/zoom ? Commented May 27, 2010 at 0:41
  • Thankyou! It seems like I can't think at the moment. Commented May 27, 2010 at 9:28
  • 1
    Thanks for the above code. I have been searching for this type of examples from past 2 days. I have implement a custom canvas and drawing the grids as shown. The problem is that it is very slow. When I maximize or resize the window, the performance is really very slow. It hang a bit. Is there any solution to that? Commented Oct 18, 2010 at 10:02

1 Answer 1

1

As the comments to the original post state. The Pen thickness should be set to 1.0/zoom.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.