我有一个带有ScrollViewer控件的WPF窗口,其中包含许多垂直扩展的子控件。当用户单击位于ScrollViewer底部的按钮时,我希望将所有内容(当前在视图中和视图外)保存为图像。
我正在使用以下代码,我已经从示例中修改了如何保存窗口的内容:
public static void SaveForm(ScrollViewer container, string filename)
{
const int dpi = 96;
var rtb = new RenderTargetBitmap(
(int)container.ExtentWidth, //width
(int)container.ExtentHeight, //height
dpi, //dpi x
dpi, //dpi y
PixelFormats.Pbgra32 // pixelformat
);
rtb.Render(container);
SaveRTBAsPNG(rtb, filename);
}
private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
{
var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
using (var stm = System.IO.File.Create(filename))
{
enc.Save(stm);
}
}
目前正在制作PNG,但它只有ScrollViewer的当前可见部分。有什么方法可以让PNG包含所有内容,包括需要滚动到视图中的内容吗?
答案 0 :(得分:9)
将此选项与您的scrollviewer的 CONTENT 用作源,而不是您的scrollviewer本身: (即使内容不可见,它也会拍摄快照)
public static void SnapShotPNG(this UIElement source, Uri destination, int zoom)
{
try
{
double actualHeight = source.RenderSize.Height;
double actualWidth = source.RenderSize.Width;
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.LocalPath, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
}
}
catch (Exception e)
{
MessageBox.Show(e);
}
}
答案 1 :(得分:0)
这里我有用于将canvas导出为图像的代码。我想,如果你从Canvas改为ScrollViewer,它也适用于ScrollViewer。使用此代码,您还可以保存所有视图。
public static void ExportToImage(Canvas canvas)
{
dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
dlg.DefaultExt = "png";
dlg.FilterIndex = 2;
dlg.FileName = "DesignerImage.png";
dlg.RestoreDirectory = true;
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
string path = dlg.FileName;
int selectedFilterIndex = dlg.FilterIndex;
if(result==true)
{
try
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)canvas.ActualWidth, (int)canvas.ActualHeight,
96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas.Measure(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight));
canvas.Arrange(new Rect(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight)));
renderBitmap.Render(canvas);
BitmapEncoder imageEncoder = new PngBitmapEncoder();
if (selectedFilterIndex == 0)
{
}
else if (selectedFilterIndex == 1)
{
imageEncoder = new JpegBitmapEncoder();
}
else if (selectedFilterIndex == 2)
{
imageEncoder = new PngBitmapEncoder();
}
else if (selectedFilterIndex == 3)
{
imageEncoder = new JpegBitmapEncoder();
}
else if (selectedFilterIndex == 4)
{
imageEncoder = new GifBitmapEncoder();
}
imageEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
using (FileStream file = File.Create(path))
{
imageEncoder.Save(file);
}
}
catch (Exception ex)
{
}
}
}