Hi,
I have implemented a custom canvas in WPF with c#. The canvas will be keeping custom drawing shapes. I have also added a zooming functionality to it. When the canvas is zoomed at very high level, I want to calculate the visible part of the canvas so that I can perform operation on that much portion of the canvas keeping the best performance.
I also want to draw grids around the pixels. Is there any way to calculate the width of the one pixel when zoomed-in?
Following is my code for zooming and drawing grid:
class MyC: Canvas
{
ScaleTransform st = new ScaleTransform();
double zoomLevel = 1.1;
public MyC()
{
st.ScaleX = 1;
st.ScaleY = 1;
this.RenderTransform = st;
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
Pen pen = new Pen(new SolidColorBrush(Colors.Silver), 1/zoomLevel);
pen.DashStyle = DashStyles.Dash;
for (double x = 0; x < this.ActualWidth; x += 1)
{
drawingContext.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight));
}
for (double y = 0; y < this.ActualHeight; y += 1)
{
drawingContext.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
}
}
protected override void OnMouseWheel(System.Windows.Input.MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (e.Delta > 0)
{
zoomLevel *= 1.1;
zoomLevel *= 1.1;
}
else
{
zoomLevel /= 1.1;
zoomLevel /= 1.1;
}
st.ScaleY = zoomLevel;
st.ScaleX = zoomLevel;
}
}