如何使用C#创建gif图像 - c#

我想用几个框架创建一个gif文件。
我想使用Microsoft支持的方法-Image.SaveAdd

但是我不知道如何设置EncoderParameters参数以构成gif文件。

我找不到要参考的文件。那么如何使用Image.SaveAdd创建gif文件

参考方案

可能为时已晚,无法用于原始海报,但是我设法仅使用System.Drawing创建了适当的gif。下面的代码基于jschroedl的答案,但是还设置了帧延迟和动画循环数。

// Gdi+ constants absent from System.Drawing.
const int PropertyTagFrameDelay = 0x5100;
const int PropertyTagLoopCount = 0x5101;
const short PropertyTagTypeLong = 4;
const short PropertyTagTypeShort = 3;

const inr UintBytes = 4;

//...

var gifEncoder = GetEncoder(ImageFormat.Gif);
// Params of the first frame.
var encoderParams1 = new EncoderParameters(1);
encoderParams1.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
// Params of other frames.
var encoderParamsN = new EncoderParameters(1);
encoderParamsN.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionTime);            
// Params for the finalizing call.
var encoderParamsFlush = new EncoderParameters(1);
encoderParamsFlush.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);

// PropertyItem for the frame delay (apparently, no other way to create a fresh instance).
var frameDelay = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
frameDelay.Id = PropertyTagFrameDelay;
frameDelay.Type = PropertyTagTypeLong;
// Length of the value in bytes.
frameDelay.Len = Bitmaps.Count * UintBytes;
// The value is an array of 4-byte entries: one per frame.
// Every entry is the frame delay in 1/100-s of a second, in little endian.
frameDelay.Value = new byte[Bitmaps.Count * UintBytes];
// E.g., here, we're setting the delay of every frame to 1 second.
var frameDelayBytes = BitConverter.GetBytes((uint)100);
for (int j = 0; j < Bitmaps.Count; ++j)
    Array.Copy(frameDelayBytes, 0, frameDelay.Value, j * UintBytes, UintBytes);

// PropertyItem for the number of animation loops.
var loopPropertyItem = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
loopPropertyItem.Id = PropertyTagLoopCount;
loopPropertyItem.Type = PropertyTagTypeShort;
loopPropertyItem.Len = 1;
// 0 means to animate forever.
loopPropertyItem.Value = BitConverter.GetBytes((ushort)0);

using (var stream = new FileStream("animation.gif", FileMode.Create))
{
    bool first = true;
    Bitmap firstBitmap = null;
    // Bitmaps is a collection of Bitmap instances that'll become gif frames.
    foreach (var bitmap in Bitmaps)
    {
        if (first)
        {
            firstBitmap = bitmap;
            firstBitmap.SetPropertyItem(frameDelay);
            firstBitmap.SetPropertyItem(loopPropertyItem);
            firstBitmap.Save(stream, gifEncoder, encoderParams1);
            first = false;
        }
        else
        {
            firstBitmap.SaveAdd(bitmap, encoderParamsN);
        }
    }
    firstBitmap.SaveAdd(encoderParamsFlush);
}

// ...

private ImageCodecInfo GetEncoder(ImageFormat format)
{
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}

在Python和C++之间传输数据而无需写入Windows和Unix文件 - python

我有预先存在的python和C ++文件,其中python文件定义了许多点,而C ++代码利用其现有库进行了所需的计算。最终产品是C ++代码写入的文件。我正在寻找一种在python中获取2000点列表的方法,将其传递给函数,然后执行所有C ++代码并输出我需要的文件。其他注意事项。这必须是可以在Linux或Windows机器上工作的东西,并且最少安装新插件…

如何锁定终端运行的perl,obj c,c++,python和ruby等脚本的源代码? - python

我想出售我在perl,obj c,c ++,python,ruby,bash,php等中制作的脚本等它们都在终端中运行。 (Linux)如何锁定源代码,以便无需人们访问源代码即可分发我的脚本..?换句话说,如何将在Terminal中运行的程序的源代码锁定,以便人们可以使用该程序(如果该代码已下载到他们的Linux机器上,但他们无法访问实际的源代码)?例:ex…

如何使用C#或C / C++在Windows 7中获取智能卡阅读器名称? - c#

我正在尝试制作一个将使用C .dll(不幸的是,.dll没有好的文档)来访问智能卡的C#程序。 .dll的功能之一使用读取器的名称作为参数。我的问题是我不知道该如何命名。在寻找答案之后,我在这里的示例中发现了与我所需要的东西类似的内容:http://msdn.microsoft.com/en-us/library/aa379803%28VS.85%29.as…

标签名称属性带有+和引号 - javascript

当数据从数据库到网格中的视图时,标签名称属性带有+和引号,但我不希望这样做:以下是我正在执行的代码@if (ViewBag.ExperienceDetailsGrid != null) { var ExprowCount = 1; Foreach (var item in ViewBag.ExperienceDetailsGrid) { <tr cla…

Javascript + Python:将数组发送到Python脚本,将结果返回给Javascript - javascript

我想建立一个网页,该网页通过Javascript API进行许多Facebook状态更新,并将它们分类到一个数组中。然后,我想将此数组发送到Python脚本,该脚本可以专门使用NLTK.进行语言分析。在Python中获得合适的结果后,我想将结果从该脚本返回到Javascript,以显示给用户等。听起来可能吗? javascript大神给出的解决方案 是的,完…