Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. multiframe gif

multiframe gif

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephane David
    wrote on last edited by
    #1

    I'm trying to maje an animation using gif. I have several frames that I want to save inmy gif file, using the SaveAdd function. But I cannot find anywhere example or documentation about the gif encoder. Can someone give my an example of how to do it, or direct me to some example? I have found only info for Tiff.

    H 1 Reply Last reply
    0
    • S Stephane David

      I'm trying to maje an animation using gif. I have several frames that I want to save inmy gif file, using the SaveAdd function. But I cannot find anywhere example or documentation about the gif encoder. Can someone give my an example of how to do it, or direct me to some example? I have found only info for Tiff.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      All the classes involved are an abstract implementation, meaning what works for one works for another (the classes used themselves, that is). If you want to know what EncoderParameters are supported, see this simple console app that I threw together quick:

      using System;
      using System.Collections;
      using System.Drawing;
      using System.Drawing.Imaging;
      using System.Reflection;

      public class Test
      {
      static void Main(string[] args)
      {
      string fe = "gif";
      if (args.Length > 0) fe = args[0];

      ImageCodecInfo\[\] codecs = ImageCodecInfo.GetImageEncoders();
      ImageCodecInfo codec = null;
      foreach (ImageCodecInfo ci in codecs)
      {
        if (ci.FilenameExtension.ToLower().IndexOf(fe.ToLower()) >= 0)
        {
          codec = ci;
          break;
        }
      }
      
      if (codec == null)
      {
        Console.Error.WriteLine("No image codec found for extension " + fe);
        return;
      }
      
      Image img = new Bitmap(1, 1);
      EncoderParameters encparams = null;
      try
      {
        encparams = img.GetEncoderParameterList(codec.Clsid);
      }
      catch (NotImplementedException)
      {
        Console.Error.WriteLine("No encoder parameters supported for extension " + fe);
        return;
      }
      
      foreach (EncoderParameter encparam in encparams.Param)
      {
        string encname = GetEncoderName(encparam.Encoder.Guid);
        Console.WriteLine("{0}: {1}", encname, encparam.Type);
      }
      

      }

      static Hashtable encnames;
      static string GetEncoderName(Guid g)
      {
      if (encnames == null)
      {
      encnames = new Hashtable();

        Type t = typeof(Encoder);
        FieldInfo\[\] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static);
        foreach (FieldInfo field in fields)
        {
          Encoder enc = (Encoder)field.GetValue(null);
          encnames\[enc.Guid\] = field.Name;
        }
      }
      
      return encnames\[g\] as string;
      

      }
      }

      If you run this, you'll see that the GIF image encoder doesn't even support any EncoderParameters, so you can just pass null.

      Microsoft MVP, Visual C# My Articles

      S 1 Reply Last reply
      0
      • H Heath Stewart

        All the classes involved are an abstract implementation, meaning what works for one works for another (the classes used themselves, that is). If you want to know what EncoderParameters are supported, see this simple console app that I threw together quick:

        using System;
        using System.Collections;
        using System.Drawing;
        using System.Drawing.Imaging;
        using System.Reflection;

        public class Test
        {
        static void Main(string[] args)
        {
        string fe = "gif";
        if (args.Length > 0) fe = args[0];

        ImageCodecInfo\[\] codecs = ImageCodecInfo.GetImageEncoders();
        ImageCodecInfo codec = null;
        foreach (ImageCodecInfo ci in codecs)
        {
          if (ci.FilenameExtension.ToLower().IndexOf(fe.ToLower()) >= 0)
          {
            codec = ci;
            break;
          }
        }
        
        if (codec == null)
        {
          Console.Error.WriteLine("No image codec found for extension " + fe);
          return;
        }
        
        Image img = new Bitmap(1, 1);
        EncoderParameters encparams = null;
        try
        {
          encparams = img.GetEncoderParameterList(codec.Clsid);
        }
        catch (NotImplementedException)
        {
          Console.Error.WriteLine("No encoder parameters supported for extension " + fe);
          return;
        }
        
        foreach (EncoderParameter encparam in encparams.Param)
        {
          string encname = GetEncoderName(encparam.Encoder.Guid);
          Console.WriteLine("{0}: {1}", encname, encparam.Type);
        }
        

        }

        static Hashtable encnames;
        static string GetEncoderName(Guid g)
        {
        if (encnames == null)
        {
        encnames = new Hashtable();

          Type t = typeof(Encoder);
          FieldInfo\[\] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static);
          foreach (FieldInfo field in fields)
          {
            Encoder enc = (Encoder)field.GetValue(null);
            encnames\[enc.Guid\] = field.Name;
          }
        }
        
        return encnames\[g\] as string;
        

        }
        }

        If you run this, you'll see that the GIF image encoder doesn't even support any EncoderParameters, so you can just pass null.

        Microsoft MVP, Visual C# My Articles

        S Offline
        S Offline
        Stephane David
        wrote on last edited by
        #3

        It doesn't work. preview.Save(fileName, myImageCodecInfo, myEncoderParameters); // OK preview.SaveAdd(frame,null); // Wrong parameters used (sorry, the exact error message is in French). I've tried to use null for myEncoderParameters in the Save, same result Also same result if I don't use the CodecInfo at all. The code works with TIFF. Not with gif. Any idea?

        H 1 Reply Last reply
        0
        • S Stephane David

          It doesn't work. preview.Save(fileName, myImageCodecInfo, myEncoderParameters); // OK preview.SaveAdd(frame,null); // Wrong parameters used (sorry, the exact error message is in French). I've tried to use null for myEncoderParameters in the Save, same result Also same result if I don't use the CodecInfo at all. The code works with TIFF. Not with gif. Any idea?

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Of course passing null or a null reference results in the same problem - it's the same exact thing! As I said before, the whole thing is pretty abstract, so you take use the concepts for the TIFF example (wherever you got that) and apply it to saving a frame with a GIF image. The documentation has more information about the EncoderParameters that are supported by GDI+. The Encoder class has several static fields with various pre-defined Encoders that you can use in the EncoderParameters. See the documentation for them in the .NET Framework SDK. The code I posted shows that the GIF image encoder doesn't support any Encoders, so i don't know why passing null doesn't work. Google should find some examples if you use the right keywords.

          Microsoft MVP, Visual C# My Articles

          S 1 Reply Last reply
          0
          • H Heath Stewart

            Of course passing null or a null reference results in the same problem - it's the same exact thing! As I said before, the whole thing is pretty abstract, so you take use the concepts for the TIFF example (wherever you got that) and apply it to saving a frame with a GIF image. The documentation has more information about the EncoderParameters that are supported by GDI+. The Encoder class has several static fields with various pre-defined Encoders that you can use in the EncoderParameters. See the documentation for them in the .NET Framework SDK. The code I posted shows that the GIF image encoder doesn't support any Encoders, so i don't know why passing null doesn't work. Google should find some examples if you use the right keywords.

            Microsoft MVP, Visual C# My Articles

            S Offline
            S Offline
            Stephane David
            wrote on last edited by
            #5

            http://www.dotnet247.com/247reference/msgs/28/140380.aspx it seems only the TIFF encoder supports multipages...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups