Create a screenshot and save as PDF using C#


A screenshot, also known as a screen capture, or screen grab, is a digital image that shows the contents of a computer display or another device (phone, tablet). The screenshot is created by the operating system or software running on the device powering the display.

This means that the image will show your desktop, open applications, email with correspondence, which tabs are open on your computer or laptop, which document you are currently viewing.

By default, the screenshot covers the entire page on the screen. Programmatically, you can set the desired area or crop the image in a third-party application.

Screenshots are used for technical assistance in the support service, information exchange between employees on the homework and telework. Also, screenshots are often used to confirm payment of bills, personal or business correspondence, indicate the location on the map, pictures from surveillance cameras.

The code example below shows how to create a screenshot and save it to PDF format.


           using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using SautinSoft;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)

        {
            // Convert Screenshot to PNG file.
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            string screen = @"screenshot.png";
            v.PageStyle.PageSize.Auto();

            // Create screen with 1920*1040 px.
            Rectangle rect = new Rectangle(0, 0, 1920, 1040);
            Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(rect.Right, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
            bmp.Save(screen, ImageFormat.Png);

            // Create object of Image class from file.
            System.Drawing.Image image = Image.FromFile(screen);

            byte[] imgBytes = null;

            using (MemoryStream ms = new System.IO.MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                imgBytes = ms.ToArray();
            }

            // Convert image stream to PDF file.
            FileInfo outFile = new FileInfo(@"Result.pdf");
            int ret = v.ConvertImageStreamToPDFFile(imgBytes, outFile.FullName);
            if (ret == 0)
            {
                // Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile.FullName) { UseShellExecute = true });
            }
        }
    }
}
                   

The result is a screenshot of the desktop in PNG and PDF formats for further use.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.