0

我使用这些代码来打印 UI。打印出来是有效的,但如果纸张大小超过了,UI 会在画布中间切断。

有没有什么办法可以不中途截断?

<--cs 代码-->

PrintDialog dialog = new PrintDialog();
dialog.PrintVisual(lst , "print");

<--Xaml -->

<ListView Name="lst">
    <Grid Name="grdPrint">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Canvas  Grid.Row="0"    >
           .......
        </Canvas>

        <HListBox x:Name="lstImage" ItemsSource="{Binding IMG, Mode=TwoWay}" Grid.Row="1" IsHitTestVisible="True">
            <HListBox.ItemTemplate>
                <DataTemplate>
                    <HImage Margin="0"  Width="590"  Height="590" Stretch="Fill" Source="{Binding IMG_PATH_NM, Converter={StaticResource StrUriConverter}}" Tag="{Binding IMG_PATH_NM}">
                    </HImage>
                </DataTemplate>
            </HListBox.ItemTemplate>
            <HListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Center"  IsHitTestVisible="True"/>
                </ItemsPanelTemplate>
            </HListBox.ItemsPanel>
        </HListBox>
    </Grid>
</ListView>
4

2 回答 2

2

此方法会将画布打印为 PNG 文件。

public void ExportToPNG(string imgpath, Canvas surface)
{
    Uri path = new Uri(imgpath);

    if (path == null)
        return;
    Transform transform = surface.LayoutTransform;
    surface.LayoutTransform = null;

    Size size = new Size(surface.Width, surface.Height);
    surface.Measure(size);
    surface.Arrange(new Rect(size));

    RenderTargetBitmap renderBitmap =
        new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height,
        96d,
        96d,
        PixelFormats.Pbgra32);
    renderBitmap.Render(surface);

    using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        encoder.Save(outStream);
    }
    surface.LayoutTransform = transform;
}
于 2013-01-04T08:54:59.223 回答
0

您可以创建一个 BitmapImage(请参阅 RenderTargetBitmap 从元素创建位图)。然后可以将此位图保存为 JPEG 文件并使用 GDI+ (System.Image) 进行操作。

于 2013-01-04T06:40:48.423 回答