如何在.NET中生成GIF图像?

5

1
需要使用.gif格式图片有什么特殊原因吗?我能想到的唯一理由是如果需要使图像动画化。如果不需要,其他格式(如png和jpg)提供更好的压缩比率和更大的灵活性... - Blank
没有特别的原因。GIF是一种简单的无损格式,具有广泛的网络浏览器兼容性。 - Zack Peterson
8个回答

12
Bitmap bmp = new Bitmap(xSize, ySize, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp)) {
  // Use g and/or bmp to set pixels, draw lines, show text, etc...
}
bmp.Save(filename, ImageFormat.Gif);

工作完成


无论是否被接受,这似乎都不允许在GIF图像中添加动画。(虽然我认为网络不需要更多的动画,但为了完整起见。) - Blank
制作动画GIF需要什么? - Zack Peterson
.NET不支持编码动画GIF图像。请参见http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET。 - JYelton
System.Windows.Media.Imaging.GifBitmapEncoder有什么问题吗?它在WPF中默认可用。 - fireydude
NGif是什么 https://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET? - Kiquenet
显示剩余3条评论

4
请注意,除了bmp.Save(filename, ImageFormat.Gif);方法之外,还有一个bmp.Save(stream, ImageFormat.Gif);方法可以让你创建并输出图像到网页上,而无需将其保存到服务器硬盘。

2
这里是使用 System.Drawing 命名空间中的类来实现此操作的开端。它绘制了一条带有两个框的线条,以展示对形状的支持,而不仅仅是设置像素。
// add a reference to System.Drawing.dll
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap(400, 100);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(Brushes.White, 0.0f, 0.0f, 400f, 100f);

                // draw line
                using (Pen p = new Pen(Color.Black, 1.0f))
                {
                    g.DrawLine(p, 0, 49, 399, 49);
                }

                // Draw boxes at start and end
                g.FillRectangle(Brushes.Blue, 0, 47, 5, 5);
                g.FillRectangle(Brushes.Blue, 394, 47, 5, 5);
            }


            bmp.Save("test.gif", ImageFormat.Gif);
            bmp.Dispose();
        }
    }
}

1

为什么不直接使用System.Drawing命名空间呢?你需要的一切都应该在里面。


1

使用HTTPHandler创建图像并通过流发送的示例(使用已在此处发布的图像代码)。

用法:

<img src="createChart.ashx?data=1"/>

代码:

public class CreateChart : IHttpHandler
{
     public void ProcessRequest(HttpContext context)
     {
        string data = context.QueryString["data"]; // Or get it from a POST etc

        Bitmap image = new Bitmap(xSize, ySize, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(Image)) 
        {
           // Use g to set pixels, draw lines, show text, etc...
        }
        BinaryStream s = new BinaryStream();

        image.Save(s, ImageFormat.Gif);

        context.Response.Clear();
        context.Response.ContentType = "image/gif";
        context.Response.BinaryWrite(s);
        context.Response.End();
     }

     public bool IsReusable { get { return false; } }
}

实际上,您应该能够跳过BinaryStream并直接将其写入Reposnse: bmp.Save(Response.OutputStream, ImageFormat.Gif); - James Curran

0

为什么不使用图表控件而是尝试生成GIF?除非需求严格要求生成这个特定的GIF,否则我认为使用图表控件提供更多的灵活性。


不是的。但问题并没有限制答案只能涉及.NET Framework。它说的是“.net库”。 - Tundey
实际上,现在是:http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx - Omer Bokhari

0

我喜欢我们公司的产品。您将能够读写GIF并从头开始创建它们。它在桌面应用程序中运行时免版税,适用于服务器许可。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接