保存的ListView整体形象 [英] Save image of whole ListView

查看:114
本文介绍了保存的ListView整体形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码:

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?

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?

推荐答案

我已经改变了你的第一个两行,通过增加安排和措施方法,其允许控制在存储器渲染。我认为,你的控件不水平滚动,不停​​的宽度,因为它是,因为否则将用于其最大的孩子需要的最小宽度。你可以改变它。

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.

下面是你的方法。

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

这篇关于保存的ListView整体形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆