保存整个ListView的图像

时间:2015-09-21 09:09:12

标签: c# wpf

我有下一个代码:

private static void SnapShotPNG(ListView source, string destination, int zoom)
{
    try
    {
        double actualHeight = source.ActualHeight;
        double actualWidth = source.ActualWidth;

        double renderHeight = actualHeight * zoom;
        double renderWidth = actualWidth * zoom;

        RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
        VisualBrush sourceBrush = new VisualBrush(source);

        DrawingVisual drawingVisual = new DrawingVisual();
        DrawingContext drawingContext = drawingVisual.RenderOpen();

        using (drawingContext)
        {
            drawingContext.PushTransform(new ScaleTransform(zoom, zoom));
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
        }
        renderTarget.Render(drawingVisual);

        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderTarget));
        using (FileStream stream = new FileStream(destination, FileMode.Create, FileAccess.Write))
        {
            encoder.Save(stream);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

它将给定的源保存到图像,它工作正常。但它只保存控件的可见部分(在我的例子中,只保存ListView的可见项)。如何在ListView中保存整个项目的快照?

1 个答案:

答案 0 :(得分:2)

我通过添加“排列和测量”方法更改了前两行,这些方法允许控件在内存中呈现。我假设,你的控件不会水平滚动并保持宽度,因为否则它将使用其最大的孩子所需的最小宽度。你可以改变它。

这是你的方法。

   private static void SnapShotPNG(ListView source, string destination, int zoom)
        {
            try
            {
                double actualWidth = source.ActualWidth;
                source.Measure(new Size(source.ActualWidth, Double.PositiveInfinity));
                source.Arrange(new Rect(0, 0, actualWidth, source.DesiredSize.Height));
                double actualHeight = source.ActualHeight;

                double renderHeight = actualHeight * zoom;
                double renderWidth = actualWidth * zoom;

                RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
                VisualBrush sourceBrush = new VisualBrush(source);

                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();

                using (drawingContext)
                {
                    drawingContext.PushTransform(new ScaleTransform(zoom, zoom));
                    drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
                }
                renderTarget.Render(drawingVisual);

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                using (FileStream stream = new FileStream(destination, FileMode.Create, FileAccess.Write))
                {
                    encoder.Save(stream);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }