question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked Viorel-1 commented

How can I convert Gif images in folder that are in Bit depth 24 to Gif images but Bit depth 8 ?

I'm getting gif images from a folder

 var test = System.IO.Directory.GetFiles(@"D:\Downloaded Images\New folder",
                                      "*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();


I want to convert this gif images to Bit depth 8

I tried this method but it's just converting the gif images to bitmap images type they are still Bit depth 24 and now they are Bitmap.

 for (int i = 0; i < test.Length; i++)
             {
                 var bmp = new Bitmap(test[i]);
                 ConvertTo1Or8Bit.ColorToGrayscale(new Bitmap(bmp));
                 bmp.Dispose();
             }

The class code, suppose to convert but it does nothing but changing the gif images to bitmap type.

 using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
    
 namespace Extract
 {
     class ConvertTo1Or8Bit
     {
         public static Bitmap ColorToGrayscale(Bitmap bmp)
         {
             int w = bmp.Width,
             h = bmp.Height,
             r, ic, oc, bmpStride, outputStride, bytesPerPixel;
             PixelFormat pfIn = bmp.PixelFormat;
             ColorPalette palette;
             Bitmap output;
             BitmapData bmpData, outputData;
    
             //Create the new bitmap
             output = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
    
             //Build a grayscale color Palette
             palette = output.Palette;
             for (int i = 0; i < 256; i++)
             {
                 Color tmp = Color.FromArgb(255, i, i, i);
                 palette.Entries[i] = Color.FromArgb(255, i, i, i);
             }
             output.Palette = palette;
    
             //No need to convert formats if already in 8 bit
             if (pfIn == PixelFormat.Format8bppIndexed)
             {
                 output = (Bitmap)bmp.Clone();
    
                 //Make sure the palette is a grayscale palette and not some other
                 //8-bit indexed palette
                 output.Palette = palette;
    
                 return output;
             }
    
             //Get the number of bytes per pixel
             switch (pfIn)
             {
                 case PixelFormat.Format24bppRgb: bytesPerPixel = 3; break;
                 case PixelFormat.Format32bppArgb: bytesPerPixel = 4; break;
                 case PixelFormat.Format32bppRgb: bytesPerPixel = 4; break;
                 default: throw new InvalidOperationException("Image format not supported");
             }
    
             //Lock the images
             bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,
             pfIn);
             outputData = output.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly,
             PixelFormat.Format8bppIndexed);
             bmpStride = bmpData.Stride;
             outputStride = outputData.Stride;
    
             //Traverse each pixel of the image
             unsafe
             {
                 byte* bmpPtr = (byte*)bmpData.Scan0.ToPointer(),
                 outputPtr = (byte*)outputData.Scan0.ToPointer();
    
                 if (bytesPerPixel == 3)
                 {
                     //Convert the pixel to it's luminance using the formula:
                     // L = .299*R + .587*G + .114*B
                     //Note that ic is the input column and oc is the output column
                     for (r = 0; r < h; r++)
                         for (ic = oc = 0; oc < w; ic += 3, ++oc)
                             outputPtr[r * outputStride + oc] = (byte)(int)
                             (0.299f * bmpPtr[r * bmpStride + ic] +
                             0.587f * bmpPtr[r * bmpStride + ic + 1] +
                             0.114f * bmpPtr[r * bmpStride + ic + 2]);
                 }
                 else //bytesPerPixel == 4
                 {
                     //Convert the pixel to it's luminance using the formula:
                     // L = alpha * (.299*R + .587*G + .114*B)
                     //Note that ic is the input column and oc is the output column
                     for (r = 0; r < h; r++)
                         for (ic = oc = 0; oc < w; ic += 4, ++oc)
                             outputPtr[r * outputStride + oc] = (byte)(int)
                             ((bmpPtr[r * bmpStride + ic] / 255.0f) *
                             (0.299f * bmpPtr[r * bmpStride + ic + 1] +
                             0.587f * bmpPtr[r * bmpStride + ic + 2] +
                             0.114f * bmpPtr[r * bmpStride + ic + 3]));
                 }
             }
    
             //Unlock the images
             bmp.UnlockBits(bmpData);
             output.UnlockBits(outputData);
    
             return output;
         }
    
    
         public static void BitmapToGIF(Bitmap BitmapFile, string GIFFile)
         {
             using (Bitmap bitMap = new Bitmap(BitmapFile))
             {
                 //var codecInfo = GetEncoderInfo(ImageFormat.Gif);
                 var codecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatID == ImageFormat.Gif.Guid);
    
                 using (var paramsEncoder = new EncoderParameters(2))
                 {
                     paramsEncoder.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
                     paramsEncoder.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
                     //bitMap.Save(GIFFile, codecInfo, paramsEncoder);
                 }
             }
         }
     }
 }


The goal is to convert the gif images to gif images with Bit depth 8 not to create new gif images but to convert this in the array test.

dotnet-csharpwindows-forms
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Gif images in folder that are in Bit depth 24

24bpp GIF ?

A GIF has 8bpp max : Graphics File Formats

"GIFs store at most 8 bits per pixel, so they are limited to 256 colors"



0 Votes 0 ·

I downloaded them that way.

145722-nogif1.jpg




The downloaded images are not gif's ? So I need to convert them to real gif's ? I'm downloading the images and save them as .gif

0 Votes 0 ·
nogif1.jpg (89.0 KiB)

If they are real GIF, there should be GIF89 in the header

With a 24bpp JPG named as .gif, this simply converts it to real GIF

 Image img = Image.FromFile("E:\\testfake24bpp.gif");
 img.Save("E:\\test8bpp.gif", System.Drawing.Imaging.ImageFormat.Gif);
1 Vote 1 ·

Probably you are not saving the downloaded images as GIF. What code are you using? Maybe it can be adjusted to avoid the additional conversion.


1 Vote 1 ·
Show more comments

0 Answers