views:

617

answers:

8
A: 

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
Is that part of the .NET framework?
Zack Peterson
No. But the question didn't limit answers to .NET Framework. It said a ".net library".
Tundey
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
ob
A: 

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
+1  A: 

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

Alex Fort
+11  A: 
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

Chris
Exactly. Read up on the System.Drawing namespace to understand how to draw the lines needed etc.
Chris Pebble
You da man. If this doesn't get the check, I'm quitting SO.
MusiGenesis
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
I stand suitably corrected.The comment should read 'Use g and/or bmp to ...'
Chris
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.)
Nicholas Flynt
What does it take to make animated GIFs?
Zack Peterson
+4  A: 

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
+2  A: 

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
A: 

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

Zack Peterson
+1  A: 

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
Actually, you should be able to skip the BinaryStream and write it directly to the Reposnse:bmp.Save(Response.OutputStream, ImageFormat.Gif);
James Curran