1

我正在尝试缩放 InkCanvas 的内容 (Strokes) 以适应固定的打印页面大小。我想从 InkCanvas 中裁剪出所有周围的空白,并在保持纵横比的同时放大 Strokes 以适应页面。

在标记下方的处理程序中,您可以看到我正在更改从 800x300 开始的网格尺寸,并将其设置为 425x550,是可打印页面大小的一半。

标记:

 <Grid>

    <Button Height="100" Width="100" HorizontalAlignment="Left" PreviewMouseLeftButtonDown="Button_PreviewMouseLeftButtonDown_1" />

    <Grid Width="1200" Height="1400" Background="Aquamarine" HorizontalAlignment="Right" VerticalAlignment="Top">

        <Grid Background="Red" x:Name="grid" Width="800" Height="300">

            <Viewbox x:Name="vb"  Width="800" Height="300" Stretch="Fill" StretchDirection="Both">

                <InkCanvas Width="500" Height="500" Background="Transparent" IsEnabled="True"/>
            </Viewbox>

        </Grid >

    </Grid>
</Grid>

代码隐藏文件:

bool b = true;

private void Button_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
    {
        if (b)
            {
            //I toyed with using Uniform, UniformToFill, and Fill
            vb.Stretch = Stretch.Fill;
            grid.Width = 425;
            grid.Height = 550;

            //scale viewbox down until it fits horizontally
            var scaleX = grid.Width / vb.Width;
            vb.Width *= scaleX;
            vb.Height *= scaleX;

            //if constraining it to the width made it larger than it needed to be vertically, scale it down
            if (vb.Height > grid.Height)
            {
                var scaleY = grid.Height / vb.Height;
                vb.Width *= scaleY;
                vb.Height *= scaleY;
            }
            b = false;
        }
        else
        {
           //reset it back to what it was
            vb.Stretch = Stretch.Fill;
            grid.Width = 800;
            grid.Height = 300;
            vb.Width = grid.Width;
            vb.Height = grid.Height;
            b = true;
        }
4

0 回答 0