Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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

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

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top