计算WPF中图像的可见区域

时间:2010-10-18 09:43:33

标签: c# wpf canvas

我用WP#在WPF中实现了一个自定义画布。画布将保持自定义绘图形状。我还为它添加了缩放功能。当画布放大到非常高的水平时,我想计算画布的可见部分,这样我就可以在画布的大部分上执行操作,保持最佳性能。

我还想在像素周围绘制网格。有什么方法可以在放大时计算一个像素的宽度?

以下是我的缩放和绘制网格代码:

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

0 个答案:

没有答案