|
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
namespace ScreenToPDF
{
public partial class Form1 : Form
{
ScreenCapture capScreen = new ScreenCapture();
public Form1()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void captureScreen()
{
try
{
// Call the CaptureAndSave method from the ScreenCapture class
// And create a temporary file in C:\Temp
capScreen.CaptureAndSave(@"C:\Temp\test.png", CaptureMode.Window, ImageFormat.Png);
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
private void btnExport_Click(object sender, EventArgs e)
{
// Call your captureScreen() function
captureScreen();
// Create new pdf document and page
PdfDocument doc = new PdfDocument();
PdfPage oPage = new PdfPage();
// Add the page to the pdf document and add the captured image to it
doc.Pages.Add(oPage);
XGraphics xgr = XGraphics.FromPdfPage(oPage);
XImage img = XImage.FromFile(@"C:\Temp\test.png");
xgr.DrawImage(img, 0, 0);
saveFileDialog.Filter = ("PDF File|*.pdf");
DialogResult btnSave = saveFileDialog.ShowDialog();
if (btnSave.Equals(DialogResult.OK))
{
doc.Save(saveFileDialog.FileName);
doc.Close();
}
// I used the Dispose() function to be able to
// save the same form again, in case some values have changed.
// When I didn't use the function, an GDI+ error occurred.
img.Dispose();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.