Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 缩放InkCanvas笔划以适合特定尺寸,以裁剪出未使用的空间_Wpf_Scale_Viewbox_Inkcanvas - Fatal编程技术网

Wpf 缩放InkCanvas笔划以适合特定尺寸,以裁剪出未使用的空间

Wpf 缩放InkCanvas笔划以适合特定尺寸,以裁剪出未使用的空间,wpf,scale,viewbox,inkcanvas,Wpf,Scale,Viewbox,Inkcanvas,我正在尝试缩放InkCanvas的内容(笔划)以适合打印的固定页面大小。我想从InkCanvas中裁剪出所有周围的空白,并放大笔划以适应页面,同时保持纵横比 在标记下面的处理程序中,您可以看到我正在更改从800x300开始的网格的尺寸,并将其设置为425x550,即可打印页面大小的一半 标记: <Grid> <Button Height="100" Width="100" HorizontalAlignment="Left" PreviewMouseLeftButt

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

在标记下面的处理程序中,您可以看到我正在更改从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;
        }