1

I am working on changing all image formats into .gif format, but it's losing its quality after changing to a new format. I used the following code for changing formats.

String paths = HttpContext.Current.Request.PhysicalApplicationPath + "DesktopModules\\DNAiusParallelSlider\\Images\\"+ModuleId.ToString()+"\\";
if (FileUpload2.HasFile)
{
    string extension = Path.GetExtension(FileUpload2.PostedFile.FileName);
    FileUpload2.SaveAs(paths + "waves" + extension);
    System.Drawing.Image image1 = System.Drawing.Image.FromFile(@paths + "\\waves" + extension);
    System.Drawing.Image img = System.Drawing.Image.FromFile(@paths + "\\waves" + extension);
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
    {
        image1.Save(@paths + "\\waves.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }
    // Save the image in PNG format.
}
2
  • 1
    Might be because the GIF format only supports 256 colours.
    – Maarten
    Jul 11, 2012 at 11:26
  • 1
    Also, remember that the jpeg and (as far as I can tell) wmf and emf formats are lossy and will lose quality when you save them in the first place. Also, saving a jpeg as a gif is a great way to waste the potential of the compression algorithm.
    – Tharwen
    Jul 11, 2012 at 11:30

1 Answer 1

1

I think its inevitable that you will lose quality when converting to gif, especially from

JPEG and PNG. This is because Gif is an 8 bit format which means the maximum number of colours supported by the format is 256.

So you may lose fine shades in difference of colors because of approximation.

GIF is for images with only a few distinct colors, such as line drawings and simple cartoons.

If you still doubt your c# code, you can go to this online image converter to check the quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.