---
title: "Auto-invert image and PDF C# .NET (JPG, TIFF, PNG) | Nutrient .NET SDK"
canonical_url: "https://www.nutrient.io/guides/dotnet/ocr/preprocess/auto-invert/"
md_url: "https://www.nutrient.io/guides/dotnet/ocr/preprocess/auto-invert.md"
last_updated: "2026-05-15T19:10:04.988Z"
description: "Learn how to auto-invert image and PDF colors in C# using Nutrient .NET SDK. Improve optical character recognition (OCR) accuracy with automatic color inversion techniques."
---

# Auto-invert images and PDFs in C#

Negative images and documents have reverse or inverted colors. For example, the text is white and the background is black. This can be an issue because intelligent document recognition assumes that images and documents have normal colors. This guide explains how to automatically detect if images and documents have inverted colors and how to reverse the colors.

To automatically detect inverted colors in an image and reverse them, 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. Check if the image has inverted colors by passing the image ID to the `IsNegative` method of the `GdPictureImaging` object.

4. If the source image has inverted colors, reverse the colors by passing the image ID to the `FxNegative` method of the `GdPictureImaging` object.

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

6. Release the image resource with the `ReleaseGdPictureImage` method of the `GdPictureImaging` object.

The example below checks if the colors are inverted in an image file. If the colors are inverted, the example reverses the colors and saves the output in a new image file:

### C#

```csharp

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the image from a file.
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png");
// Check if the image has inverted colors.
bool isNegative = gdpictureImaging.IsNegative(imageId);
// Check if the image has inverted colors.
if (isNegative)
{
    // Reverse the colors.
    gdpictureImaging.FxNegative(imageId);
    gdpictureImaging.SaveAsPNG(imageId, @"C:/temp/output.png");
}
// Release the image resource.
gdpictureImaging.ReleaseGdPictureImage(imageId);

```

### VB.NET

```vb

Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Load the image from a file.
    Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:/temp/source.png")
    ' Check if the image has inverted colors.
    Dim isNegative As Boolean = gdpictureImaging.IsNegative(imageId)
    ' Check if the image has inverted colors.
    If isNegative Then
        ' Reverse the colors.
        gdpictureImaging.FxNegative(imageId)
        gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png")
    End If
    ' Release the image resource.
    gdpictureImaging.ReleaseGdPictureImage(imageId)
End Using

```

#### Used methods

- [`CreateGdPictureImageFromFile`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~CreateGdPictureImageFromFile.html)

- [`FxNegative`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~FxNegative.html)

- [`IsNegative`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~IsNegative.html)

- [`ReleaseGdPictureImage`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~ReleaseGdPictureImage.html)

- [`SaveAsPNG`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~SaveAsPNG.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)

---

## Related pages

- [Remove lines from images and PDFs in C#](/guides/dotnet/ocr/preprocess/remove-lines.md)
- [Image preprocessing and OCR accuracy in C#](/guides/dotnet/ocr/preprocess.md)
- [Deskew PDFs and images in C#](/guides/dotnet/ocr/preprocess/deskew.md)
- [Remove noise from images in C#](/guides/dotnet/ocr/preprocess/remove-noise.md)
- [Enhance characters in PDFs and images in C#](/guides/dotnet/ocr/preprocess/enhance-characters.md)
- [Remove punch holes from images and PDFs in C#](/guides/dotnet/ocr/preprocess/remove-punch-holes.md)

