Link to home
Start Free TrialLog in
Avatar of oferz
oferz

asked on

Convert image from 24 BPP to 8 BPP

Hello experts,

I have a Grayscale 24 BPP, I need to save it as 8 BPP for other application that are using 8 BPP.
Does someone here have a sample code or can direct me to solving this issue?

Thanks in advance,
Ofer
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

I use this method:
{
    class ImageHandling
    {
        //http://blog.paranoidferret.com/index.php/2007/08/31/csharp-tutorial-convert-a-color-image-to-greyscale/

        public static Bitmap MakeGrayscale3(Bitmap original)
        {
            // Create a blank bitmap the same size as original.
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            // Get a graphics object from the new image.
            Graphics graphic = Graphics.FromImage(newBitmap);

            // Create the grayscale ColorMatrix.
            ColorMatrix colorMatrix = new ColorMatrix(new float[][] 
              {
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
              });

            // Create image attributes.
            ImageAttributes attributes = new ImageAttributes();

            // Set the color matrix attribute.
            attributes.SetColorMatrix(colorMatrix);

            // Draw the original image on the new image
            // using the grayscale color matrix.
            graphic.DrawImage(original,
               new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height,
               GraphicsUnit.Pixel, attributes);

            // Dispose the Graphics object.
            graphic.Dispose();
            return newBitmap;
        }
    }
}

Open in new window

The link is dead, and I haven't bothered how many BPP is creates, but I see only three byte values other than Black and White, so it may be 8 BPP.

/gustav
Hi Ofer,

You can do this with a command line call to the excellent (and free!) GraphicsMagick software. This EE article explains how to download and install it:
https://www.experts-exchange.com/articles/18119/Reduce-the-file-size-of-many-JPG-files-in-many-folders-via-an-automated-mass-batch-compression-method.html

The command line you need is this:

gm.exe convert -depth 8 input.jpg output.jpg

Of course, the file types don't have to be JPG. Here's the doc on convert:
http://www.graphicsmagick.org/convert.html

And here's the doc on -depth:
http://www.graphicsmagick.org/GraphicsMagick.html#details-depth

I see that two of your Topics are C# and Visual Basic.NET, which is fine. You may utilize <gm.exe> from any programming/scripting language that allows you to invoke a command line call. Regards, Joe
Avatar of oferz
oferz

ASKER

Thank you very much guys but it didn't help me.
The first solution changes the image to 32 BPP (I'm looking for a way to save the image as 8 BPP)
The second solution is not .net code, i do not want to use a thrid party exe, Im looking for an integrated solution via .Net.

Thank you very much and I will be glad to get more suggestions.
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial