Taking a screenshot is a quick and easy way to capture and share information. While Windows provides a snipping tool, it can only capture and save images. With this code, you can save the image as well as the text in the image. This text can be indexed so you can find the information later using Windows built-in search.
With .NET’s Clipboard Class
, developers can easily use the GetImage
Method to retrieve an image stored in your clipboard. Once you have the image from your clipboard, use LEADTOOLS to convert the image to a RasterImage
using the ConvertFromImage
Method. You can now convert the new RasterImage
to a searchable PDF using the LEADTOOLS OCR SDK.
Code
.NET code to get the image from the clipboard
public Image GetClipboardImage()
{
Image returnImage = null;
if (Clipboard.ContainsImage())
{
returnImage = Clipboard.GetImage();
}
return returnImage;
}
LEADTOOLS OCR code to save the image from the clipboard as a searchable PDF
string outputPath = $@"C:\Temp\{textBox1.Text}.pdf";
Image screenshot = GetClipboardImage();
using (RasterImage image = RasterImageConverter.ConvertFromImage(screenshot, ConvertFromImageOptions.None))
{
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
{
ocrEngine.Startup(codecs, null, null, null;
// Create an OCR document
using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument())
{
// Add this image to the document
IOcrPage ocrPage = ocrDocument.Pages.AddPage(image, null);
// Auto-recognize the zones in the page
ocrPage.AutoZone(null);
// Recognize it and save it as a PDF file
ocrPage.Recognize(null);
ocrDocument.Save(outputPath, DocumentFormat.Pdf, null);
}
}
}
Try it!
Try this for free by downloading the LEADTOOLS free evaluation!
Support
Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.