5

Is there a .NET library I can use to programmatically generate my own GIF images?

At a minimum I'd like to build it pixel-by-pixel. Better would be support for text and shapes.

Here's an example of what I'm trying to do. I mocked this up in Photoshop…

Number line graphic http://img143.imageshack.us/img143/5458/dollarlineot9.gif

What do you recommend?

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
  • 1
    Is there any particular reason is needs to be a .gif format image? The only reason I can think of for using a .gif is if the image needed to be animated. If not, other formats (such as png and jpg) offer much better compression rates and more flexibility... – Blank Dec 08 '08 at 18:55
  • No particular reason. GIF is a simple lossless format with wide web browser compatibility. – Zack Peterson Dec 08 '08 at 19:27

8 Answers8

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);

Job done

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
Chris
  • 1,685
  • 10
  • 15
  • Exactly. Read up on the System.Drawing namespace to understand how to draw the lines needed etc. – Chris Van Opstal Dec 08 '08 at 18:29
  • Although technically you can't really set pixels with the Graphics object (you can fill 1-pixel rectangles). SetPixel and GetPixel are on the Bitmap object. – MusiGenesis Dec 08 '08 at 18:32
  • I stand suitably corrected. The comment should read 'Use g and/or bmp to ...' – Chris Dec 08 '08 at 18:33
  • Accepted or not, this doesn't seem to allow for animation in GIF images. (Not that I think the web needs more of them, but for the sake of completion.) – Blank Dec 08 '08 at 18:54
  • What does it take to make animated GIFs? – Zack Peterson Dec 08 '08 at 19:03
  • .NET doesn't support encoding animated GIF images. See http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET – JYelton Aug 25 '12 at 00:31
  • What is wrong with System.Windows.Media.Imaging.GifBitmapEncoder? It is available from WPF by default. – fireydude May 16 '13 at 22:10
  • Whats about ***NGif*** https://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET ? – Kiquenet Dec 26 '16 at 12:06
4

note that in addition to the bmp.Save(filename, ImageFormat.Gif); method, there is a bmp.Save(stream, ImageFormat.Gif); which allow you to create & output an image to a webpage, without it ever to saved to your servers hard disk.

James Curran
  • 101,701
  • 37
  • 181
  • 258
2

Here's the beginnings of doing so with the classes in the System.Drawing namespace. It draws a line with two boxes to demonstrate support for shapes at a higher level than simply setting pixels.

// 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();
        }
    }
}
OwenP
  • 24,950
  • 13
  • 65
  • 102
1

Why not just use the System.Drawing namespace? Everything you need should be in there.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Alex Fort
  • 18,459
  • 5
  • 42
  • 51
1

An example of how to use a HTTPHandler to create the image and send out in a stream (using the image code already posted here).

Use:

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

Code:

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; } }
}
FlySwat
  • 172,459
  • 74
  • 246
  • 311
  • Actually, you should be able to skip the BinaryStream and write it directly to the Reposnse: bmp.Save(Response.OutputStream, ImageFormat.Gif); – James Curran Dec 08 '08 at 18:50
0

Why not use a chart control instead of trying to generate GIFs? Unless the requirements are strictly to generate this particular GIF, I think using a chart control offers you more flexibility.

Tundey
  • 2,926
  • 1
  • 23
  • 27
  • No. But the question didn't limit answers to .NET Framework. It said a ".net library". – Tundey Dec 08 '08 at 18:36
  • actually, it is now: 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 Feb 27 '09 at 20:38
0

I like my company's products. You will be able read/write gifs and create them from whole cloth. It's runtime royalty free for desktop apps, licensed for servers.

plinth
  • 48,267
  • 11
  • 78
  • 120
0

Generating Images On-the-Fly with ASP.NET (Feb 22, 2002) by Stephen Walther

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280