Crop Images in C# .NET
To crop images, use the following methods:
-
Crop
crops an image by specifying coordinates and dimensions. -
CropTop, CropRight, CropBottom, CropLeft
crops the individual sides of an image. -
CropBorders
removes an image background of any color. -
CropBorders
with reference parameters gets the dimensions of the image background. -
CropWhiteBorders
removes a white image background. -
CropBlackBorders
removes a black image background. -
CropBlackBordersEx
changes a black image background to white. -
DeleteBlackBorders
removes a black image background with irregular shapes.
Cropping an Image
To crop an image, use the Crop
method. The input parameters are the following:
-
The coordinates of the top-left corner of the cropped area.
-
The width of the cropped area.
-
The height of the cropped area.
The origin of the coordinate system is the top-left corner of the original image.
To crop an image by 5 percent on all sides, use the following example:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); int height = gdpictureImaging.GetHeight(imageID); int width = gdpictureImaging.GetWidth(imageID); // Crop the image by 5 percent on all sides. gdpictureImaging.Crop(imageID, width * 5 / 100, height * 5 / 100, width * 90 / 100, height * 90 / 100); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") Dim height As Integer = gdpictureImaging.GetHeight(imageID) Dim width As Integer = gdpictureImaging.GetWidth(imageID) ' Crop the image by 5 percent on all sides. gdpictureImaging.Crop(imageID, width * 5 / 100, height * 5 / 100, width * 90 / 100, height * 90 / 100) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Cropping Individual Sides
To crop an image from an individual side, use the following methods:
Use the Lines
parameter to specify how many pixels to crop.
To crop an image from the bottom by 5 percent, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); int height = gdpictureImaging.GetHeight(imageID); // Crop the image by 5 percent from the bottom. gdpictureImaging.CropBottom(imageID, height * 5 / 100); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") Dim height As Integer = gdpictureImaging.GetHeight(imageID) ' Crop the image by 5 percent from the bottom. gdpictureImaging.CropBottom(imageID, height * 5 / 100) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Cropping an Image Background
The CropBorders
method detects and removes the background of an image of any color. It accepts the following parameters:
-
ImigingContext
sets the image type. -
Optional:
Confidence
sets the confidence level for deleting a single line of a background. -
Optional:
ReferencePoint
sets the color reference for determining the dominant background color.
To delete the background of an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and remove the background of a photo image. gdpictureImaging.CropBorders(imageID, ImagingContext.ContextPhoto); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and remove the background of a photo image. gdpictureImaging.CropBorders(imageID, ImagingContext.ContextPhoto) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Getting Crop Area Dimensions
To get the dimensions of the cropped area, use the CropBorders
method and the following reference parameters:
-
Left
is the X coordinate of the top-left corner. -
Top
is the Y coordinate of the top-left corner. -
Width
is the width of the cropped area. -
Height
is the height of the cropped area.
The origin of the coordinate system is the top-left corner of the original image.
If you reference the listed parameters, the CropBorders
method only detects the crop area and calculates the cropped area. It doesn’t delete the background. The CropBorders
method accepts the following parameters:
-
Confidence
sets the confidence level for deleting a single line of a background. -
ReferencePoint
sets the color reference for determining the dominant background color. -
Optional:
ImigingContext
sets the image type.
To get the dimensions of the cropped area, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); int left = 0, top = 0, width = 0, height = 0; // Detect the crop area with 50 percent confidence level. gdpictureImaging.CropBorders(imageID, 50, ReferencePoint.ReferencePointBottomRight, ref left, ref top, ref width, ref height); Console.WriteLine(left + ", " + top + ", " + width + ", " + height); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") Dim left = 0, top = 0, width = 0, height = 0 ' Detect the crop area with 50 percent confidence level. gdpictureImaging.CropBorders(imageID, 50, ReferencePoint.ReferencePointBottomRight, left, top, width, height) Console.WriteLine(left & ", " & top & ", " & width & ", " & height) gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Related Topics
Cropping a White Background
The CropWhiteBorders
method detects and removes the white background of an image. It accepts the following parameters:
-
Confidence
sets the confidence level for deleting a single line of a background. -
SkipLinesCount
specifies how many lines to skip.
To delete the white background of an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and remove the white background. gdpictureImaging.CropWhiteBorders(imageID); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and remove the white background. gdpictureImaging.CropWhiteBorders(imageID) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Cropping a Black Background
The CropBlackBorders
method detects and removes the black background of an image. It accepts the following parameters:
-
Confidence
sets the confidence level for deleting a single line of a background. -
SkipLinesCount
specifies how many lines to skip.
To delete the black background of an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and remove the black background. gdpictureImaging.CropBlackBorders(imageID); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and remove the black background. gdpictureImaging.CropBlackBorders(imageID) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Changing a Black Background Color
To change the black background color to white, use the CropBlackBordersEx
method. It doesn’t remove the background, which means that the dimensions of the image are kept. This method uses two optional parameters:
-
Confidence
sets the confidence level for deleting a single line of a background. -
SkipLinesCount
specifies how many lines to skip.
This method only detects rectangular backgrounds. For irregular shapes, use the
DeleteBlackBorders
method.
To change the black background of an image to white, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and change the background color to white. gdpictureImaging.CropBlackBordersEx(imageID); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and change the background color to white. gdpictureImaging.CropBlackBordersEx(imageID) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Changing a Black Background Color for Irregular Shapes
The DeleteBlackBorders
method is used for images with an irregular black background. The Margin
parameter works as a safety buffer, and it sets the maximum distance in pixels from the edges of the image where a border is allowed to start. The default value is 10
. Set the SkewedBorders
parameter to true
to handle irregular shapes.
To delete a black background with an irregular shape, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Replace the black background from a skewed image by replacing it with white content. // The maximum distance from edges of an image where a border is allowed to start is 20 pixels. gdpictureImaging.DeleteBlackBorders(imageID, Margin:20, SkewedBorders:true); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Replace the black background from a skewed image by replacing it with white content. ' The maximum distance from edges of an image where a border is allowed to start is 20 pixels. gdpictureImaging.DeleteBlackBorders(imageID, Margin:=20, SkewedBorders:=True) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Confidence Level
Use the Confidence
parameter to set the confidence level of the following methods:
If the line exceeds the specified confidence level, it’s removed or modified. Otherwise, it’s left untouched. The Confidence
parameter ranges between 0 and 100 percent. The default value is 75
.
To lower the confidence level of the CropBlackBorders
method to 50 percent, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and remove the background with 50 percent confidence level. gdpictureImaging.CropBlackBorders(imageID, 50); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and remove the background with 50 percent confidence level. gdpictureImaging.CropBlackBorders(imageID, 50) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Skipping Lines
Use the SkipLinesCount
parameter to specify how many lines to skip when using one of the following methods:
To use this parameter, specify the Confidence
parameter:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Detect and remove the background with 75 percent confidence level and skip 3 lines. gdpictureImaging.CropBlackBorders(imageID, Confidence:75, SkipLinesCount:3); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Detect and remove the background with 75 percent confidence level and skip 3 lines. gdpictureImaging.CropBlackBorders(imageID, Confidence:=75, SkipLinesCount:=3) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Reference Point
Use the ReferencePoint
parameter to specify the dominant color of the background of the image. The possible values are the following:
-
ReferencePointBottomLeft
-
ReferencePointBottomRight
-
ReferencePointTopLeft
-
ReferencePointTopRight
The following functionalities can use this parameter:
Imaging Context
Use the ImigingContext
parameter to specify the image type. The possible values are the following:
-
ContextDocument
-
ContextPhoto
-
ContextUnknown
The following methods can use this parameter: