multiframe gif
-
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.
-
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.
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
EncoderParameter
s 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 passnull
.Microsoft MVP, Visual C# My Articles
-
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
EncoderParameter
s 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 passnull
.Microsoft MVP, Visual C# My Articles
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?
-
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?
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 theEncoderParameter
s that are supported by GDI+. TheEncoder
class has several static fields with various pre-definedEncoder
s that you can use in theEncoderParameter
s. See the documentation for them in the .NET Framework SDK. The code I posted shows that the GIF image encoder doesn't support anyEncoder
s, so i don't know why passingnull
doesn't work. Google should find some examples if you use the right keywords.Microsoft MVP, Visual C# My Articles
-
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 theEncoderParameter
s that are supported by GDI+. TheEncoder
class has several static fields with various pre-definedEncoder
s that you can use in theEncoderParameter
s. See the documentation for them in the .NET Framework SDK. The code I posted shows that the GIF image encoder doesn't support anyEncoder
s, so i don't know why passingnull
doesn't work. Google should find some examples if you use the right keywords.Microsoft MVP, Visual C# My Articles
http://www.dotnet247.com/247reference/msgs/28/140380.aspx it seems only the TIFF encoder supports multipages...