c# - 保存整个ListView的图像

标签 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 中保存整个项目的快照?

最佳答案

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

这是您的方法。

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

关于c# - 保存整个ListView的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32691326/

相关文章:

c# - 将选择设置为 View 框

c# - 有没有办法在 protobuf-net 代理类中定义替代转换函数(从/到接口(interface))

wpf - 是否可以使用样式或模板应用混合行为?

wpf - 在 xaml 中将 View 绑定(bind)到 ViewModel 会引发错误 "Cannot create an instance of"

c# - 满足条件时 CanExecute() 不启用按钮

wpf - UserControl 根据数据类型更新图像

c# - 如何在 c# 中为 mysql 处理空日期而不将它们转换为空

c# - 通过代码将自定义链接添加到 SharePoint 列表设置页面

c# - 从电子邮件 C# 中获取收件人列表

c# - 需要向矩形添加文本