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.

Skewed before image Deskewed after image

Information

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.

  1. Create a GdPictureImaging object.

  2. Select the image by passing its path to the CreateGdPictureImageFromFile method of the GdPictureImaging object.

  3. Deskew the image with the AutoDeskew method of the GdPictureImaging object. This method takes the following parameters:

    1. The image ID.

    2. 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.

  4. Save the output in a new image with the SaveAsPNG method of the GdPictureImaging object.

  5. Release the image resource with the ReleaseGdPictureImage method of the GdPictureImaging 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
Used Methods

Related Topics