列表框导出到图片

时间:2018-07-23 07:49:25

标签: c# wpf xaml

我对将ListBox导出到图像文件有疑问。

我在XAML中有以下代码:

 <ListBox SelectedItem="{Binding SelectedObject}" 
                 PreviewMouseMove="ListBox_PreviewMouseMove"
                 PreviewMouseDown="ListBox_PreviewMouseDown" Name="platno">
            <ListBox.Template>
                <ControlTemplate>

                    <Border>
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" Opacity=".3">
                                <GradientStop Color="White" Offset="0"/>
                                <GradientStop Color="GhostWhite" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>

                        <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" x:Name="scroll">
                            <Border>
                                <!-- Pozadí s mřížkou -->
                                <Border.Background>
                                    <VisualBrush TileMode="Tile"
                                    Viewport="0,0,50,50" ViewportUnits="Absolute" 
                                    Viewbox="0,0,50,50" ViewboxUnits="Absolute">
                                        <VisualBrush.Visual>
                                            <Rectangle Stroke="Darkgray" StrokeThickness="1" Height="50" Width="50"
                                        StrokeDashArray="5 3"/>
                                        </VisualBrush.Visual>
                                    </VisualBrush>
                                </Border.Background>
                                <ItemsPresenter/>
                            </Border>
                        </ScrollViewer>

                    </Border>

                </ControlTemplate>

            </ListBox.Template>

            <ListBox.ItemsSource>
                <StaticResource ResourceKey="Col"/>
            </ListBox.ItemsSource>

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True" Background="#01FFFFFF" 
                            Height="{Binding AreaHeight}" Width="{Binding AreaWidth}"
                            VerticalAlignment="Top" HorizontalAlignment="Left" x:Name="hlavniPlatno"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                    <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyFocusVisualStyle}"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <ContentPresenter x:Name="Content"/>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Content" Property="Effect">
                                            <Setter.Value>
                                                <DropShadowEffect Color="Gray" ShadowDepth="4" BlurRadius="10"/>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                    <DataTrigger Binding="{Binding IsNew}" Value="True">
                                        <Setter Property="Opacity" Value=".5"/>
                                    </DataTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

具有this视觉效果。现在我的问题。我在后面的代码可以导出中央面板,而程序的顶部和左侧没有面板。我在导出列表框内的对象时遇到麻烦。当我尝试将此片段导出到图片时,我仅给出场景this之类的缩放部分以及后面的方法

 private void CreateJPG(string path) {

        var vm = DataContext as MainViewModel;
        var print = platno;

        RenderTargetBitmap rb = new RenderTargetBitmap(Convert.ToInt32(vm.AreaWidth), Convert.ToInt32(vm.AreaHeight), 150d, 150d, PixelFormats.Pbgra32);
        rb.Render(print);

        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(rb));



        using (FileStream fs = new FileStream(path, FileMode.Create)) {
            encoder.Save(fs);
            fs.Flush();
            fs.Close();
        }

    }

我的目标是将列表框(或ScrollView)中的过度思想导出到没有缩放因子的图像。

1 个答案:

答案 0 :(得分:1)

如果您不希望缩放,则应在RenderTargetBitmap中使用96 dpi:

RenderTargetBitmap rb = new RenderTargetBitmap(Convert.ToInt32(vm.AreaWidth), Convert.ToInt32(vm.AreaHeight), 96d, 96d, PixelFormats.Pbgra32);