有没有一个.NET库可以用来以编程方式生成自己的GIF图像?
至少我想逐像素构建它。更好的是支持文本和形状。
这是我尝试做的一个示例。我在Photoshop中模拟了这个…
数行图形http://img143.imageshack.us/img143/5458/dollarlineot9.gif
你有什么建议吗?
有没有一个.NET库可以用来以编程方式生成自己的GIF图像?
至少我想逐像素构建它。更好的是支持文本和形状。
这是我尝试做的一个示例。我在Photoshop中模拟了这个…
数行图形http://img143.imageshack.us/img143/5458/dollarlineot9.gif
你有什么建议吗?
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);
工作完成
bmp.Save(filename, ImageFormat.Gif);
方法之外,还有一个bmp.Save(stream, ImageFormat.Gif);
方法可以让你创建并输出图像到网页上,而无需将其保存到服务器硬盘。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();
}
}
}
为什么不直接使用System.Drawing命名空间呢?你需要的一切都应该在里面。
使用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; } }
}
为什么不使用图表控件而是尝试生成GIF?除非需求严格要求生成这个特定的GIF,否则我认为使用图表控件提供更多的灵活性。