2

I have the next code:

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);
    }
}

It saves a given source to an image, it works fine. But it save only visible part of control (in my case, only visible items of ListView). How can i save snapshot of whole items in ListView?

Mohit S
  • 13,723
  • 6
  • 34
  • 69
l1pton17
  • 444
  • 4
  • 16

1 Answers1

2

I've changed your first two lines, by adding Arrange and Measure methods, which allow the control to render in memory. I've assumed, that your control doesn't scroll horizontally and kept the width as it was, since otherwise it will use minimal width required for its largest child. You can change it.

Here is your method.

   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);
            }
        }
netaholic
  • 1,345
  • 10
  • 22