Convert Images to Grayscale in C#

convert image to grayscale C#

In various cases, you may need to convert the color images to grayscale or black and white, such as, in image processing, etc. Therefore, in this article, you will learn how to convert color images to grayscale using C#. Furthermore, the article will cover grayscaling and binarization of the images explicitly.

C# API for Converting Image to Grayscale

In order to convert RGB color images to grayscale, we’ll use Aspose.Imaging for .NET. It is a powerful image processing API that lets you manipulate popular image formats seamlessly. You can either download the API or install it using NuGet.

PM> Install-Package Aspose.Imaging

Convert Images to Grayscale in C#

The following are the steps to convert a color image to grayscale in C#.

The following code sample shows how to convert an image to grayscale using C#.

Input Image

image to grayscale C#

Converted to Grayscale

grayscale image

Convert Image to Black and White with Binarization

In binarization, each pixel in an image can have only two possible values; 0 or 1. Here, 0 denotes the absence and 1 denotes the presence of the color. Aspose.Imaging supports two binarization methods i.e. binarization with fixed and binarization with Otsu threshold.

Binarization with Fixed Threshold in C#

The following are the steps to perform binarization on an image using fixed threshold.

The following C# code applies binarization to an image and converts it to black and white.

Binarization with Otsu Threshold in C#

The following are the step to convert an image to black and white with Otsu threshold.

The following code sample shows how to convert an image to black and white with Otsu threshold using C#.

Get a Free API License

You can get a free temporary license in order to use the API without evaluation limitations.

Conclusion

In this article, you have learned how to convert an image to grayscale and black and white using C#. Moreover, you have seen how to apply binarization to the images with fixed or Otsu threshold. You can explore more about the C# image processing API using documentation. In addition, you can share your queries with us via our forum.

See Also