gpt4 book ai didi

c# - 保存整个 ListView 的图像

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:00 34 4
gpt4 key购买 nike

我有下一个代码:

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

最佳答案

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

这是您的方法。

   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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com