Crop images in C# .NET
To crop images, use the following methods:
Cropcrops an image by specifying coordinates and dimensions.CropTop, CropRight, CropBottom, CropLeftcrops the individual sides of an image.CropBordersremoves an image background of any color.CropBorderswith reference parameters gets the dimensions of the image background.CropWhiteBordersremoves a white image background.CropBlackBordersremoves a black image background.CropBlackBordersExchanges a black image background to white.DeleteBlackBordersremoves 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 UsingCropping 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 UsingCropping an image background
The CropBorders method detects and removes the background of an image of any color. It accepts the following parameters:
ImigingContextsets the image type.- Optional:
Confidencesets the confidence level for deleting a single line of a background. - Optional:
ReferencePointsets 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 UsingGetting crop area dimensions
To get the dimensions of the cropped area, use the CropBorders method and the following reference parameters:
Leftis the X coordinate of the top-left corner.Topis the Y coordinate of the top-left corner.Widthis the width of the cropped area.Heightis 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:
Confidencesets the confidence level for deleting a single line of a background.ReferencePointsets the color reference for determining the dominant background color.- Optional:
ImigingContextsets 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 UsingCropping a white background
The CropWhiteBorders method detects and removes the white background of an image. It accepts the following parameters:
Confidencesets the confidence level for deleting a single line of a background.SkipLinesCountspecifies 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 UsingCropping a black background
The CropBlackBorders method detects and removes the black background of an image. It accepts the following parameters:
Confidencesets the confidence level for deleting a single line of a background.SkipLinesCountspecifies 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 UsingChanging 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:
Confidencesets the confidence level for deleting a single line of a background.SkipLinesCountspecifies 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 UsingChanging 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 UsingConfidence 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 UsingSkipping 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 UsingReference point
Use the ReferencePoint parameter to specify the dominant color of the background of the image. The possible values are the following:
ReferencePointBottomLeftReferencePointBottomRightReferencePointTopLeftReferencePointTopRight
The following functionalities can use this parameter:
Imaging context
Use the ImigingContext parameter to specify the image type. The possible values are the following:
ContextDocumentContextPhotoContextUnknown
The following methods can use this parameter: