0

I am trying to convert png image to gif and jpg format. I am using the code that I've found at Microsoft documentation.

I've created git-hub example by modifying this code into this:

        public static void Main(string[] args)
        {
            // Load the image.
            using (Image png = Image.FromFile("test-image.png"))
            {
                var withBackground = SetWhiteBackground(png);
                // Save the image in JPEG format.
                withBackground.Save("test-image.jpg");

                // Save the image in GIF format.
                withBackground.Save("test-image.gif");
                withBackground.Dispose();
            }
        }

        private static Image SetWhiteBackground(Image img)
        {
            Bitmap imgWithBackground = new Bitmap(img.Width, img.Height);
            Rectangle rect = new Rectangle(Point.Empty, img.Size);
            using (Graphics g = Graphics.FromImage(imgWithBackground))
            {
                g.Clear(Color.White);
                g.DrawImageUnscaledAndClipped(img, rect);
            }

            return imgWithBackground;
        }

Original image (fictional data) is this: Swiss bill with fictional data

And when I convert it to gif I get this: enter image description here

So my question: is there a way to get gif format out of png that would look the same?

Edit: Hans Passant pointed out that the root problem was transparent background. After some digging I found answer here. I used code snipped mentioned in the link to set background to white:

        private Image SetWhiteBackground(Image img)
        {
            Bitmap imgWithBackground = new Bitmap(img.Width, img.Height);
            Rectangle rect = new Rectangle(Point.Empty, img.Size);
            using (Graphics g = Graphics.FromImage(imgWithBackground))
            {
                g.Clear(Color.White);
                g.DrawImageUnscaledAndClipped(img, rect);
            }

            return imgWithBackground;
        }

So now the gif looks like this: Gif image with white background

ogiz
  • 33
  • 1
  • 7
  • Sometimes GDI have problems converting images. Did you try a library like ImageSharp? – Sha Apr 16 '20 at 12:32
  • The png is unusual, it is black text on a transparent background. Only the center of the QR code has white pixels. JPEG doesn't support transparency and the GIF encoder doesn't let you pick the transparency color, so you end up with black text on a black background. Workaround is to provide a well-defined background, use Graphics.DrawImage() to draw this image on a bitmap that's cleared to white. But realistically you'd consider fixing the problem at the source, black text on transparent background is a bug. – Hans Passant Apr 16 '20 at 12:41

1 Answers1

3

Something like (https://docs.sixlabors.com/articles/imagesharp/gettingstarted.html):

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// Open the file and detect the file type and decode it.
// Our image is now in an uncompressed, file format agnostic, structure in-memory as a series of pixels.
using (Image image = Image.Load("test-image.png")) 
{     
    // The library automatically picks an encoder based on the file extensions then encodes and write the data to disk.
    image.Save("test.gif"); 
} 
Sha
  • 2,185
  • 1
  • 36
  • 61