如何获取Windows窗体程序的屏幕截图并保存为pdf ???
ive尝试了以下代码:输出为白页
// generate a file name as the current date/time in unix timestamp format
string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
PrintDocument doc = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = Path.Combine(directory, file + ".pdf"),
}
};
doc.PrintPage+= new PrintPageEventHandler(PrintImage);
CaptureScreen();
doc.Print();
答案 0 :(得分:0)
我解决了这个问题。 (使用iTextSharp) 这是我的代码:
Graphics g = this.CreateGraphics();
bmp = new Bitmap(this.Size.Width, this.Size.Height + 10, g);
Graphics mg = Graphics.FromImage(bmp);
mg.CopyFromScreen(this.Location.X + 20, this.Location.Y + 25 ,40, 47, this.Size);
// Save the screenshot to the specified path that the user has chosen.
bmp.Save("Screenshot.png", ImageFormat.Png);
Document document = new Document();
document.SetMargins(1,1,1,1);
using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream("Screenshot.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = iTextSharp.text.Image.GetInstance(imageStream);
float maxWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
float maxHeight = document.PageSize.Height - document.TopMargin - document.BottomMargin;
if (image.Height > maxHeight || image.Width > maxWidth)
image.ScaleToFit(maxWidth, maxHeight);
document.Add(image);
}
document.Close();
}