开发者

Calculate the visible area of the image in WPF

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);开发者_StackOverflow中文版

        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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜