【问题标题】:Compress animated gif image size using c#使用 c# 压缩动画 gif 图像大小
【发布时间】:2016-11-17 19:55:52
【问题描述】:

我想使用 c# 从几张图像创建动画 gif 图像,所以我使用了下面的 github 解决方案。

https://github.com/DataDink/Bumpkit

我正在使用下面的代码来做到这一点

using (var gif = File.OpenWrite(@"C:\IMG_TEST.gif"))
using (var encoder = new GifEncoder(gif))
    for (int i = 0, count = imageFilePaths.Length; i < count; i++)
    {
        Image image = Image.FromFile(imageFilePaths[i]);
        encoder.AddFrame(image,0,0);
    }

它就像一个魅力,但它正在创建大小为 45 MB 的 gif。如果我检查我的实际图像大小,那么它只有 11MB,总共 47 张图像。但不知何故,生成的 gif 尺寸很大。

现在我想压缩使用 c# 创建的 gif 图像的大小。

那么有什么办法可以压缩gif图片的大小吗?

【问题讨论】:

  • 在谷歌上搜索如何在 C# 中创建一个 zip 文件,网上有很多工作示例,也可以使用 { } 正确格式化你的第二个文件
  • 我不想创建 zip,我想压缩正在创建的动画 gif 的大小
  • 然后谷歌搜索如何压缩 gif C# 的大小
  • 这个问题会对你有所帮助。它适用于 JPG,但它很容易适应处理 GIF:stackoverflow.com/questions/1669850/…。它不适用于有问题的图书馆,但没有真正需要使用它。反正已经存在于 C# 中了。

标签: c# image animation gif animated-gif


【解决方案1】:

我知道这是一个老问题,但我想我会分享这个解决方案。

我遇到了同样的问题,发现每一帧应该只包含与前一帧的像素差异。我以为编码器会为我做图像差异,但显然它没有。

所以在添加每一帧之前,我使用我写的这个方法将它与前一帧进行比较。然后我添加仅包含更改像素的结果图像。

这是我使用它的地方:https://github.com/Jay-Rad/CleanShot/blob/master/CleanShot/Classes/GIFRecorder.cs

public class ImageDiff
{
    public static Bitmap GetDifference(Bitmap bitmap1, Bitmap bitmap2)
    {
        if (bitmap1.Height != bitmap2.Height || bitmap1.Width != bitmap2.Width)
        {
            throw new Exception("Bitmaps are not of equal dimensions.");
        }
        if (!Bitmap.IsAlphaPixelFormat(bitmap1.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap2.PixelFormat) ||
            !Bitmap.IsCanonicalPixelFormat(bitmap1.PixelFormat) || !Bitmap.IsCanonicalPixelFormat(bitmap2.PixelFormat))
        {
            throw new Exception("Bitmaps must be 32 bits per pixel and contain alpha channel.");
        }
        var newImage = new Bitmap(bitmap1.Width, bitmap1.Height);

        var bd1 = bitmap1.LockBits(new System.Drawing.Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
        var bd2 = bitmap2.LockBits(new System.Drawing.Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
        // Get the address of the first line.
        IntPtr ptr1 = bd1.Scan0;
        IntPtr ptr2 = bd2.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes = Math.Abs(bd1.Stride) * bitmap1.Height;
        byte[] rgbValues1 = new byte[bytes];
        byte[] rgbValues2 = new byte[bytes];

        // Copy the RGBA values into the array.
        Marshal.Copy(ptr1, rgbValues1, 0, bytes);
        Marshal.Copy(ptr2, rgbValues2, 0, bytes);

        // Check RGBA value for each pixel.
        for (int counter = 0; counter < rgbValues1.Length - 4; counter += 4)
        {
            if (rgbValues1[counter] != rgbValues2[counter] ||
                rgbValues1[counter + 1] != rgbValues2[counter + 1] ||
                rgbValues1[counter + 2] != rgbValues2[counter + 2] ||
                rgbValues1[counter + 3] != rgbValues2[counter + 3])
            {
                // Change was found.
                var pixel = counter / 4;
                var row = (int)Math.Floor((double)pixel / bd1.Width);
                var column = pixel % bd1.Width;
                newImage.SetPixel(column, row, Color.FromArgb(rgbValues1[counter + 3], rgbValues1[counter + 2], rgbValues1[counter + 1], rgbValues1[counter]));
            }
        }
        bitmap1.UnlockBits(bd1);
        bitmap2.UnlockBits(bd2);
        return newImage;
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-07
    • 2015-06-16
    • 2020-04-27
    • 2018-08-10
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2016-01-29
    • 2013-09-03
    相关资源
    最近更新 更多