Remove Lines from Images and PDFs in C#
Some documents have lines on the page, which makes pages difficult to read. This guide explains how to automatically remove lines from your images and PDF documents.
The images below show what a document looks like before and after removing lines.
Don’t preprocess documents before recognizing text with OCR. The GdPicture.NET OCR engine preprocesses documents automatically with better results than manual preprocessing.
Removing All Lines
To automatically detect all horizontal or vertical lines in a document and remove them, follow the steps below.
-
Create a
GdPictureImaging
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Remove the lines with the
RemoveLines
method of theGdPictureImaging
object. This method takes the following parameters:-
The image ID.
-
Members of the
LineRemoveOrientation
enumeration, separated by vertical bar|
characters. This parameter specifies the types of lines that are removed.
-
-
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 removes all horizontal lines from the document:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Remove the lines. gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal); // 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") ' Remove the lines. gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using
Used Methods and Properties
Related Topics
Removing Specific Lines
To remove specific lines that meet your criteria, follow the steps below.
-
Create a
GdPictureImaging
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Remove the lines with the
RemoveLines
method of theGdPictureImaging
object. This method takes the following parameters:-
The image ID.
-
Members of the
LineRemoveOrientation
enumeration, separated by vertical bar|
characters. This parameter specifies the types of lines that are removed. -
The maximum gap between the lines. Lines with more space between them aren’t removed.
-
The maximum thickness of the lines. Thicker lines aren’t removed.
-
The minimum length of the lines. Shorter lines aren’t removed.
-
The maximum character interception of the lines. Lines with more character interception aren’t removed.
-
Some characters might be affected by the line removal. For example, some characters might have gaps in them where a line crossed them. Specify whether to correct these characters.
-
-
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 only removes specific horizontal and vertical lines from the document:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Remove the lines. gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal | LineRemoveOrientation.Vertical, 10, 7, 500, 5, true); // 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") ' Remove the lines. gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal, 10, 7, 500, 5, True) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using