WPF Scrollviewer arrange make it go up

avkwfej4  于 2023-08-07  发布在  Go
关注(0)|答案(1)|浏览(138)

**设置:**在我的画布中,我试图拍摄所有包含在网格中的画布元素的照片。在网格中的一行内有一个“照片”按钮,它将打开对话框并拍摄画布的照片。第二行是滚动查看器显示的位置。
**问题:**我正在SO中处理此example。它在大多数情况下都能工作,但我的问题是,当照片代码触发时,特别是调用“arrange”方法时,scrollviewer在屏幕中向上移动,并与行#1处于同一水平,而它应该保持在网格的#2中。

100d1x

的字符串

**CODE:**以下是我的xaml代码:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="My Layout" Height="400" Width="420" Left="0" Top="0">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="550"  />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="300" />
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0" Grid.Column="0" x:Name="Draw" Content="Draw" FontSize="14" VerticalAlignment="Center" Click="Photo_Click" />
        <ScrollViewer Grid.Row="1" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Name="sv">
        </ScrollViewer>
    </Grid>
</Window>

字符串
我的代码背后:

private void Photo_Click(object sender, RoutedEventArgs e)
    {
        ExportToImage(sv);
    }

    public static void ExportToImage(ScrollViewer sv)
    {
        var dlg = new SaveFileDialog
        {
            Filter = "PNG Files (*.png)|*.png",
            DefaultExt = "png",
            FilterIndex = 2,
            FileName = "DesignerImage.png",
            RestoreDirectory = true
        };

        Nullable<bool> result = dlg.ShowDialog();
        string path = dlg.FileName;

        if (result == true)
        {

            try
            {
                RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
                         (int)sv.ActualWidth, (int)sv.ActualHeight,
                          96d, 96d, PixelFormats.Pbgra32);
                sv.Measure(new Size((int)sv.ActualWidth, (int)sv.ActualHeight));
                sv.Arrange(new Rect(new Size((int)sv.ActualWidth, (int)sv.ActualHeight)));

                renderBitmap.Render(sv);
                BitmapEncoder imageEncoder = new PngBitmapEncoder();
                imageEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                using (FileStream file = File.Create(path))
                {
                    imageEncoder.Save(file);

                }
            }
            catch (Exception ex)
            {

            }
        }
    }

zazmityj

zazmityj1#

为什么不使用您提供的链接中的accepted answer?它似乎比你正在使用的更适合你的需要。
以下是您的上下文中的示例实现:

xaml编码:

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="My Layout" Height="400" Width="420" Left="0" Top="0">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="550"  />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="300" />
    </Grid.ColumnDefinitions>
    <Button Grid.Row="0" Grid.Column="0" x:Name="Draw" Content="Draw" FontSize="14" VerticalAlignment="Center" Click="Photo_Click" />
    <ScrollViewer Grid.Row="1" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Name="sv">
        <StackPanel x:Name="ScrollViewerContent" Orientation="Vertical">
            <Button Height="100">1</Button>
            <Button Height="100">2</Button>
            <Button Height="100">3</Button>
            <Button Height="100">4</Button>
            <Button Height="100">5</Button>
            <Button Height="100">6</Button>
            <Button Height="100">7</Button>
            <Button Height="100">8</Button>
            <Button Height="100">9</Button>
        </StackPanel>
    </ScrollViewer>
</Grid>

字符串

后面代码:

private void Photo_Click(object sender, RoutedEventArgs e)
{
    ScrollViewerContent.SnapShotPNG(new Uri(@"C:\Temp\DesignerImage.png"), 1);
}


其中SnapShotPNG方法在扩展类中定义:

public static class WPFExtensions
{
    public static void SnapShotPNG(this UIElement source, Uri destination, int zoom)
    {
        //Code from the answer here
    }
}

相关问题