友情链接: 互动百科 CSDN.NET 百度音乐 和讯理财 世界杯吧 拉手网

asp.net用ashx画图

<%@ WebHandler Language="C#" Class="DrawLine1" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

public class DrawLine1 : IHttpHandler 
{    
    public void ProcessRequest (HttpContext context) 
    {
        List<Point> points = GetPointArray1();

        using (Bitmap ChartingImage = new Bitmap(320, 240))
        {
            using (Graphics g = Graphics.FromImage(ChartingImage))
            {
                // 指定使用平滑化处理(又称为反锯齿功能)。
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.FillRectangle(new LinearGradientBrush(new Rectangle(0, 0, 320, 240), Color.DeepPink, Color.YellowGreen, LinearGradientMode.Vertical), new Rectangle(0, 0, 320, 240));

                using (Pen p = new Pen(Color.Yellow, 2))
                {
                    if (points.Count > 1)
                    {
                        int startX = points[0].X;
                        int startY = points[0].Y;

                        for (int i = 1; i < points.Count; i++)
                        {
                            g.DrawLine(p, startX, startY, points[i].X, points[i].Y);
                            startX = points[i].X;
                            startY = points[i].Y;
                        }
                        g.DrawString("深圳 A 股:" + Convert.ToString(240 - points[points.Count - 1].Y), new Font("标楷体", 20), Brushes.Yellow, 0, 0);
                    }
                }

                using (Pen p = new Pen(Color.Red, 2))
                {
                    if (points.Count > 1)
                    {
                        int startX = points[0].X+20;
                        int startY = points[0].Y-10;

                        for (int i = 1; i < points.Count; i++)
                        {
                            g.DrawLine(p, startX, startY, points[i].X+20, points[i].Y-10);
                            startX = points[i].X+20;
                            startY = points[i].Y-10;
                        }
                        
                    }
                }

                context.Response.ContentType = "Image/png";
                context.Response.Cache.SetNoStore();
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                context.Response.Cache.SetExpires(DateTime.Now);
                context.Response.Cache.SetValidUntilExpires(false);

                using (MemoryStream ms = new MemoryStream())
                {
                    ChartingImage.Save(ms, ImageFormat.Png);
                    ms.Flush();
                    context.Response.BinaryWrite(ms.GetBuffer());
                }
            }
        }
    }

    private List<Point> GetPointArray1()
    {
        List<Point> Points = (List<Point>)(HttpContext.Current.Cache["PointArray1"]);

        if (Points == null)
        {
            Points = new List<Point>();
            Points.Add(new Point(0, 120)); // 加入第一个坐标点 (0, 120)。
            HttpContext.Current.Cache["X1"] = 0;
            HttpContext.Current.Cache["Y1"] = 120;
        }
        Random r = new Random();

        int x = Convert.ToInt32(HttpContext.Current.Cache["X1"]) + 10;
        int y = Convert.ToInt32(HttpContext.Current.Cache["Y1"]);

        if (x == 320)
        {
            x = 0;
            y = 120;
            Points.Clear();
        }

        if (y % 2 == 0)
        {
            y += r.Next(0, 11);
        }
        else
        {
            y -= r.Next(0, 11);
        }

        Points.Add(new Point(x, y));
        HttpContext.Current.Cache["PointArray1"] = Points;
        HttpContext.Current.Cache["X1"] = x;
        HttpContext.Current.Cache["Y1"] = y;
        return Points;
    }

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

posted on 2010-08-20 17:27  行万里路 责任 创新 执着  阅读(577)  评论(0编辑  收藏  举报