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
oferzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
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
Joe Winograd, Fellow&MVEDeveloperCommented:
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:
http://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
oferzAuthor Commented:
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.
Joe Winograd, Fellow&MVEDeveloperCommented:
> Im looking for an integrated solution via .Net.

.NET can make a command line call, which I consider an "integrated solution" (the command line call being integrated with other .NET code), but if you don't want a third-party EXE (or DLL), then I'm not aware of another solution to change the color depth. Regards, Joe
Gustav BrockCIOCommented:
Have a look at the two examples at the bottom here which seems to address your issue:
Convert an image to grayscale
/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.