locked
Convert PNG Images into GIF RRS feed

  • Question

  • Hi,

    I have a number of images in my localfolder of my application, is there anyway to turn these into a .GIF animation or any sort of animation combining the images?

    Thanks,

    Jamie

    Wednesday, May 22, 2013 10:46 PM

Answers

  •   using (var outStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                 {
                     var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, outStream);
    
    
                     for (int i = 0; i < nImage; i++)
                     {
                         WriteableBitmap result = eng.GetResultImage(i);
                         var pixelStream = result.PixelBuffer.AsStream();
                         byte[] pixels = new byte[pixelStream.Length];
    
                         pixelStream.Read(pixels, 0, pixels.Length);
                      
                         encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore,
                                              (uint)result.PixelWidth, (uint)result.PixelHeight,
                                              92.0, 92.0,
                                              pixels);
                         if (i == 0)
                         {
                             var properties = new BitmapPropertySet
                            {
                                {
                                    "/grctlext/Delay",
                                    new BitmapTypedValue(delayTime / 10, PropertyType.UInt16)
                                }
                            };
    
                             await encoder.BitmapProperties.SetPropertiesAsync(properties);
                         }
    
                         if (i < nImage - 1)
                             await encoder.GoToNextFrameAsync();
    
                     }
                     await encoder.FlushAsync();

    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 6:04 AM
    • Marked as answer by Jesse Jiang Tuesday, May 28, 2013 2:49 AM
    Thursday, May 23, 2013 6:09 AM

All replies

  • There isn't any built in support for GIF animations. If you just want this within your app you can load the images individually and flip through them on a timer. I haven't tried it, but I expect you could create an animated GIF by loading the images in with a BitmapDecoder and then save them as separate frames of a GIF file with a BitmapEncoder.

    --Rob

    Thursday, May 23, 2013 3:51 AM
    Moderator
  •   using (var outStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                 {
                     var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, outStream);
    
    
                     for (int i = 0; i < nImage; i++)
                     {
                         WriteableBitmap result = eng.GetResultImage(i);
                         var pixelStream = result.PixelBuffer.AsStream();
                         byte[] pixels = new byte[pixelStream.Length];
    
                         pixelStream.Read(pixels, 0, pixels.Length);
                      
                         encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore,
                                              (uint)result.PixelWidth, (uint)result.PixelHeight,
                                              92.0, 92.0,
                                              pixels);
                         if (i == 0)
                         {
                             var properties = new BitmapPropertySet
                            {
                                {
                                    "/grctlext/Delay",
                                    new BitmapTypedValue(delayTime / 10, PropertyType.UInt16)
                                }
                            };
    
                             await encoder.BitmapProperties.SetPropertiesAsync(properties);
                         }
    
                         if (i < nImage - 1)
                             await encoder.GoToNextFrameAsync();
    
                     }
                     await encoder.FlushAsync();

    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 6:04 AM
    • Marked as answer by Jesse Jiang Tuesday, May 28, 2013 2:49 AM
    Thursday, May 23, 2013 6:09 AM
  • Hi zhou lichong,

    Thank you for your reply!

    I can see the rough idea of what your saying but there are some bits missing are there? As i haven't done this before i was wondering if you could give me a more complete response?

    Thanks,

    Jamie

    Also, is this code going to work in a Metro application?
    • Edited by Jme-Jamie Thursday, May 23, 2013 6:15 AM
    Thursday, May 23, 2013 6:13 AM
  • hi jme-jamie,

    I can't provide the complete code, sorry.

    It is from one of me Windows store app, so Yes it works in a Metro application

    Thursday, May 23, 2013 9:34 AM