1
有什麼方法可以打印wpf窗口的屏幕截圖嗎?C#WPF - 打印WPF窗口的ScreenCapture
我嘗試了很多東西,但是它對A4尺寸來說太大了,或者因爲沒有設置邊距而裁剪掉了。 這裏的代碼我想:
樣本1:
private void btnPrintStanzbild_Click(object sender, RoutedEventArgs e)
{
Visual target = gRight;
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(target);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}
renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
BitmapImage bi = new BitmapImage();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (MemoryStream stm = new MemoryStream())
{
bitmapEncoder.Save(stm);
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = stm;
bi.EndInit();
}
var vis = new DrawingVisual();
var dc = vis.RenderOpen();
dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });
dc.Close();
var pdialog = new PrintDialog();
if (pdialog.ShowDialog() == true)
{
var queue = pdialog.PrintQueue;
var area = queue.GetPrintCapabilities(pdialog.PrintTicket)
.PageImageableArea;
pdialog.PrintVisual(vis, "My Image");
}
}
樣品2:
PrintDialog pdialog = new PrintDialog();
if (pdialog.ShowDialog() == true)
{
var queue = pdialog.PrintQueue;
var area = queue.GetPrintCapabilities(pdialog.PrintTicket)
.PageImageableArea;
pdialog.PrintVisual(this, "My Image");
}
示例3:
所以,你要擴大在W降低到A4尺寸並打印出來? – Sakura