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 an image to grayscale in C#. Furthermore, the article will cover grayscaling and binarization of the images explicitly.
- C# API for Converting Image to Grayscale
- Convert Image to Grayscale in C#
- Convert Image to Black and White with Binarization
- Get a Free API License
C# Image to Grayscale Converter - Free Download
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 an Image to Grayscale in C#
The following are the steps to convert a color image to grayscale in C#.
- First, load the image using the Image class.
- Cast the image into a RasterCachedImage object.
- Cache the image using RasterCachedImage.CacheData() method.
- Convert image to grayscale using RasterCachedImage.Grayscale() method.
- Finally, save the image using RasterCachedImage.Save(String) method.
The following code sample shows how to convert an image to grayscale using C#.
Input Image
The following is a sample image used in this article.
Converted to Grayscale
The following is the image after conversion to grayscale.
Save Image as Grayscale in C# - 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 a fixed threshold.
- First, load the image using the Image class.
- Cast the image into a RasterCachedImage object.
- Cache the image using RasterCachedImage.CacheData() method.
- Convert image to black and white using RasterCachedImage.BinarizeFixed() method.
- Finally, save the image using RasterCachedImage.Save(String) method.
The following C# code applies binarization to an image and converts it to black and white.
Binarization with Otsu Threshold
The following are the step to convert an image to black and white with Otsu threshold.
- First, load the image using the Image class.
- Cast the image into a RasterCachedImage object.
- Cache the image using RasterCachedImage.CacheData() method.
- Convert image to black and white using RasterCachedImage.BinarizeOtsu() method.
- Finally, save the image using RasterCachedImage.Save(String) method.
The following code sample shows how to convert an image to black and white with Otsu threshold using C#.
C# Convert Image to Grayscale with a Free License
You can get a free temporary license and convert images to grayscale 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 thresholds. Furthermore, you can explore more about the C# image processing API using documentation. Also, you can share your queries with us via our forum.