When images are stored in memory and on disk their PixelFormat has a large impact on the size of the image in bytes. A typical color image will be in 24 bits per pixel. A Grayscale image would be 8 bits per pixel and a Black and White image would be 1 bit per pixel. Sometimes images are saved in a higher pixel format than the actual data requires. This can result in the image being 8 or even 24 times as large as it needs to be. The method demonstrated here will reduce a 24bpp image down to an 8bpp image if it does not have strong color presence. This is particularly useful when trying to conserve memory and images are scanned in color and only some pages truly have color.
C#
private AtalaImage ColorCheck(AtalaImage img)
{
ColorExtractionCommand extract = new ColorExtractionCommand();
ColorExtractionResults res = extract.Apply(img) as ColorExtractionResults;
if (res.HasColor == true)
return img;
else
return img.GetChangedPixelFormat(PixelFormat.Pixel8bppGrayscale);
}
VB.NET
Private Function ColorCheck(ByVal img As AtalaImage) As AtalaImage
Dim extract As New ColorExtractionCommand()
Dim res As ColorExtractionResults = TryCast(extract.Apply(img), ColorExtractionResults)
If res.HasColor = True Then
Return img
Else
Return img.GetChangedPixelFormat(PixelFormat.Pixel8bppGrayscale)
End If
End Function
Original Article:
Q10313 - HOWTO: Determine if a 24bpp Color Image Could Be Stored as 8bpp Grayscale