Deskew PDFs and Images in C#
After scanning a physical document, the resulting image can be slightly rotated or skewed. Skewed images are difficult to read. This guide explains how to compensate for small rotations in a document by deskewing an image.
The images below show what a document looks like before and after deskewing.
![]()
Don’t preprocess documents before recognizing text with OCR. The GdPicture.NET OCR engine preprocesses documents automatically with better results than manual preprocessing.
To automatically deskew an image, follow the steps below.
-
Create a
GdPictureImaging
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Deskew the image with the
AutoDeskew
method of theGdPictureImaging
object. This method takes the following parameters:-
The image ID.
-
Optional: The maximum angle of rotation in the image. Images rotated by a larger angle aren’t deskewed. As such, this parameter is useful if you suspect that some images are accidentally skewed by a smaller angle and some images are intentionally rotated by a larger angle; you want to deskew the first group, but not the second. If you don’t specify this parameter, the maximum angle of rotation is 15 degrees by default.
-
-
Save the output in a new image with the
SaveAsPNG
method of theGdPictureImaging
object. -
Release the image resource with the
ReleaseGdPictureImage
method of theGdPictureImaging
object.
The example below automatically deskews a document:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Deskew the image. gdpictureImaging.AutoDeskew(imageId); // Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, @"C:/temp/output.png"); gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the image from a file. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:/temp/source.png") ' Deskew the image. gdpictureImaging.AutoDeskew(imageId) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using
The example below automatically deskews an image if the angle of rotation is less than 10 degrees:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Deskew the image. gdpictureImaging.AutoDeskew(imageId, 10); // Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, @"C:/temp/output.png"); gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the image from a file. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:/temp/source.png") ' Deskew the image. gdpictureImaging.AutoDeskew(imageId, 10) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using