Monday, July 18, 2022

[FIXED] Why assigning an Image object containing gif to a new instance of Bitmap, breaks the gif

Issue

An Image object contains a gif file and if saved to disk and opened, the gif is displaying properly. However, if a Bitmap object is created from the image instance, and saved, the gif no longer works:

 WebClient client = new WebClient();
 client.DownloadFile(new Uri("http://www.ajaxload.info/images/exemples/25.gif"), "25.gif");

 Image original = Image.FromFile("25.gif");
 original.Save("25-WorksFine.gif", ImageFormat.Gif);

 Bitmap bmp = new Bitmap(original);
 bmp.Save("25-Broken.gif", ImageFormat.Gif);

Solution

Image.FromFile uses native GDI+ to load a file using an appropriate decoder

new Bitmap(Image) creates a drawing surface(Graphics) of the size of the image then draws the image on it using Graphics.DrawImage

I suppose in this last case no animation is preserved



Answered By - Dmitry
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.