频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> Asp.Net教程 -> 用ASP.NET动态生成图像(转2)

用ASP.NET动态生成图像(转2)

作者:未知  来源:http://edu.chinaz.com  发布时间:2005-7-18 22:23:07  发布人:acx

减小字体 增大字体

StockPicker.aspx:
<script language="VB" runat=server>
Sub ChartBtn_Click(Sender as Object, E as EventArgs)
chart.ImageUrl = "ImageGenerator_Vb.aspx?"
chart.Visible = true
For i=0 to Stocks.Items.Count-1
If (Stocks.Items(i).Selected = true) Then
chart.ImageUrl = chart.ImageUrl & "symbols=" & Stocks.Items(i).Value & "&"
End If
Next
End Sub
</script>
<html>
<body>
<form runat=server>
<h1>Scott's Stock Picker</h1>
<asp:checkboxlist id="Stocks" runat=server>
<asp:listitem>MSFT</asp:listitem>
<asp:listitem>SUN</asp:listitem>
</asp:checkboxlist>
<asp:button text="Chart Your Selected Stocks" OnClick="ChartBtn_Click" runat=server/>
<hr>
<asp:Image id="chart" ImageUrl="" Visible=false runat=server/>
</form>
</body>
</html>

ImageGenerator_VB.aspx:
<%@ Page Language="VB" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="ChartGenerator" %>
<%@ OutputCache Duration="10" %>
<script language="VB" runat=server>
Function GetStockDetails(Symbol as String) as ChartLine
Dim myChartLine as new ChartLine
if (symbol = "msft") then
Dim StockValues() as Single = { 60, 110, 120, 180, 185, 190, 240, 290 }
myChartLine.Width = 5
myChartLine.Color = Color.Blue
myChartLine.LineStyle = DashStyle.Solid
myChartLine.Title = "Microsoft Corp. (MSFT)"
myChartLine.Symbol = "MSFT"
myChartLine.Values = StockValues
return myChartLine
elseif (symbol = "sun") then
Dim StockValues() as Single = { 180, 155, 125, 60, 25, 15, 10, 3 }
myChartLine.Width = 5
myChartLine.Color = Color.Red
myChartLine.LineStyle = DashStyle.Dot
myChartLine.Title = "Sun Corp. (Sun)"
myChartLine.Symbol = "Sun"
myChartLine.Values = StockValues
return myChartLine
end if
return nothing
End Function
Sub Page_Load(Sender as Object, E as EventArgs)
' Generate Chart Data For Image....
Dim XAxes() as String = { "9:00AM", "9:30AM", "10:00AM", "11:00AM", "12:00AM", "1:00PM", "1:30PM" }
Dim MyChartData as New ChartData
MyChartData.YTickSize = 20
MyChartData.YMax = 250
MyChartData.YMin = 0
MyChartData.XAxisTitles = XAxes
Dim Symbols() as String = Request.QueryString.GetValues("symbols")
if (Not Symbols = Nothing) then
for i=0 to Symbols.Length-1
Dim stockValue as ChartLine = GetStockDetails(symbols(i).ToLower)
If (stockValue <> nothing) then
myChartData.Lines.Add(stockValue)
End if
Next
end if
' Create In-Memory BitMap of JPEG
Dim MyChartEngine as New ChartEngine
Dim StockBitMap as BitMap = MyChartEngine.DrawChart(600, 400, myChartData)
' Render BitMap Stream Back To Client
StockBitMap.Save(Response.OutputStream, ImageFormat.JPEG)
End Sub
</script>

ChartEngine.cs:
using System.WinForms;
using System.Collections;
using System.Collections.Bases;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.ComponentModel;
using System;
using System.IO;
namespace ChartGenerator {
//Core Line Data structure
public struct LineData {
public float[] LineValues ;
public string LineTitle ;
public string LineSymbol ;
}

//Line Data plus display style information
public class ChartLine {
private Color lineColor ;
private LineData lineData ;
private DashStyle lineStyle ;
private int lineWidth ;
//Constructors
public ChartLine() :base() {}
public ChartLine(LineData lineData) :base() {
this.lineData = lineData;
}
//Properties
public Color Color {
get { return lineColor ; }
set { lineColor = value ; }
}
public DashStyle LineStyle {
get { return lineStyle ; }
set { lineStyle = value ; }
}

public string Symbol {
get { return lineData.LineSymbol ; }
set { lineData.LineSymbol = value ; }
}
public string Title {
get { return lineData.LineTitle ; }
set { lineData.LineTitle = value ; }
}
public float[] Values {
get { return lineData.LineValues ; }
set { lineData.LineValues = value ; }
}
public int Width {
get { return lineWidth ; }
set { lineWidth = value ; }
}

//Methods
public void SetLineData(LineData lineData) {
this.lineData = lineData;
}
}

//Chart Data structure
public class ChartData {
private float yTickSize;
private float yMax;
private float yMin;
private string[] xAxisTitles ;
private ChartLineList lines = new ChartLineList();
private Color gridColor=Color.Blue;
private bool showHGridLines=true;
private bool showVGridLines=true;
//Properties
public float YTickSize {
get { return yTickSize ; }
set { yTickSize = value ; }
}
public float YMax {
get { return yMax ; }
set { yMax = value ; }
}

public float YMin {
get { return yMin ; }
set { yMin = value ; }
}
public string[] XAxisTitles {
get { return xAxisTitles ; }
set { xAxisTitles = value ; }
}
public ChartLineList Lines {
get { return lines ; }
set { lines = value ; }
}
public Color GridColor {
get { return gridColor ; }
set { gridColor = value ; }
}
public bool ShowHGridLines {
get { return showHGridLines ; }
set { showHGridLines = value ; }
}
public bool ShowVGridLines {
get { return showVGridLines ; }
set { showVGridLines = value ; }
}
//Collection of Chart Lines
public class ChartLineList : TypedCollectionBase {
public ChartLine this[int index] {
get {
return (ChartLine)(List[index]);
}
set {
List[index] = value;
}
}
public int Add(ChartLine value) {
return List.Add(value);
}
public void Insert(int index, ChartLine value) {
List.Insert(index, value);
}
public int IndexOf(ChartLine value) {
return List.IndexOf(value);
}
public bool Contains(ChartLine value) {
return List.Contains(value);
}
public void Remove(ChartLine value) {
List.Remove(value);
}
public void CopyTo(ChartLine[] array, int index) {
List.CopyTo(array, index);
}
}
}

//Charting Engine - draws a chart based on the given ChartData
public class ChartEngine {
private ChartData chartData ;
private float left;
private float right;
private float top;
private float bottom;
private float tickCount;
private float yCount;
private float hspacing;
private float vspacing;
private Graphics g;
private Rectangle r;
private Color backColor;
private Color foreColor;
private Font baseFont;
private Font legendFont;
private RectangleF legendRect;
public ChartEngine() {
}
public Bitmap DrawChart(int width, int height, ChartData chartData) {
Bitmap newBitmap = new Bitmap(width,height,PixelFormat.Format32bppARGB);
Graphics g = Graphics.FromImage(newBitmap);
Rectangle r = new Rectangle(0, 0, width, height);
Color myForeColor = Color.Black;
Color myBackColor = Color.White;
Font myFont = new Font("Arial", 10);
this.DrawChart(g, r, myBackColor, myForeColor, myFont, chartData);
return newBitmap;
}
public void DrawChart(Graphics g, Rectangle r, Color backColor, Color foreColor, Font baseFont, ChartData chartData) {
this.chartData = chartData;
this.g = g;
this.r = r;
this.backColor = backColor;
this.foreColor = foreColor;
this.baseFont = baseFont;
this.legendFont = new Font(baseFont.FontFamily, (baseFont.Size * 2/3), baseFont.Style | FontStyle.Bold);
g.SmoothingMode = SmoothingMode.AntiAlias;
CalculateChartDimensions();
DrawBackground();
InternalDrawChart() ;
}

private void CalculateChartDimensions() {
right = r.Width - 5;
top = 5 * baseFont.Size ;
bottom = r.Height - baseFont.Size * 2;
tickCount = chartData.YMin ;
yCount = (chartData.YMax-chartData.YMin) / chartData.YTickSize ;
hspacing = (bottom-top) / yCount;
vspacing = (right) / chartData.XAxisTitles.Length;
//Left depends on width of text - for simplicities sake assume that largest yvalue is the biggest
//Take into account the first X Axis title
float maxYTextSize = g.MeasureString(chartData.YMax.ToString(), baseFont).Width;
float firstXTitle = g.MeasureString(chartData.XAxisTitles[0], baseFont).Width;
left = (maxYTextSize > firstXTitle) ? maxYTextSize : firstXTitle ;
left = r.X + left + 5 ;
//Calculate size of legend box
float maxLegendWidth = 0 ;
float maxLegendHeight = 0 ;
//Work out size of biggest legend
foreach (ChartLine cl in chartData.Lines) {
float currentWidth = g.MeasureString(cl.Title, legendFont).Width;
float currentHeight = g.MeasureString(cl.Title, legendFont).Height;
maxLegendWidth = (maxLegendWidth > currentWidth) ? maxLegendWidth : currentWidth ;
maxLegendHeight = (maxLegendHeight > currentHeight) ? maxLegendHeight : currentHeight ;
}
legendRect = new RectangleF(r.X+2, r.Y+2, maxLegendWidth + 25 + 5, ((maxLegendHeight+2)*chartData.Lines.Count) + 3) ;
}
private void DrawBackground() {
LinearGradientBrush b = new LinearGradientBrush(r, Color.SteelBlue, backColor,LinearGradientMode.Horizontal);
g.FillRectangle(b, r);
b.Dispose();
}
private void InternalDrawChart() {
DrawGrid() ;
foreach (ChartLine cl in chartData.Lines) {
DrawLine(cl);
}
DrawLegend() ;
//Draw time on chart
string timeString = "Generated:" + DateTime.Now.ToLongTimeString() ;
SizeF textsize = g.MeasureString(timeString,baseFont);
g.DrawString(timeString, baseFont, new SolidBrush(foreColor), r.Width - textsize.Width - 5, textsize.Height * 2 / 3) ;
}
private void DrawGrid() {
Pen gridPen = new Pen(chartData.GridColor) ;

//Vertical - include tick desc's
if (chartData.ShowVGridLines) {
for (int i = 0 ; (vspacing * i) < right; i++) {
float x = left + (vspacing *i);
string desc = chartData.XAxisTitles[i];
g.DrawLine(gridPen, x,top,x,bottom +(baseFont.Size*1/3));
SizeF textsize = g.MeasureString(desc,baseFont);
g.DrawString(desc, baseFont, new SolidBrush(chartData.GridColor), x-(textsize.Width/2), bottom + (baseFont.Size*2/3)) ;
}
}
//Horizontal
if (chartData.ShowHGridLines) {
for (float i = bottom ; i > top; i-=hspacing) {
string desc = tickCount.ToString();
tickCount+=chartData.YTickSize;
g.DrawLine(gridPen, right, i, left-3, i);
SizeF textsize = g.MeasureString(desc,baseFont);
g.DrawString(desc, baseFont, new SolidBrush(chartData.GridColor), left-textsize.Width - 3, i - (textsize.Height/2)) ;
}
}
}
private void DrawLine(ChartLine chartLine) {
Pen linePen = new Pen(chartLine.Color);
linePen.StartCap = LineCap.Round;
linePen.EndCap = LineCap.Round;
linePen.Width = chartLine.Width ;
linePen.DashStyle = chartLine.LineStyle;
PointF[] Values = new PointF[chartLine.Values.Length];
float scale = hspacing / chartData.YTickSize ;
for (int i = 0 ; i < chartLine.Values.Length; i++) {
float x = left + vspacing * i;
Values[i] = new PointF(x, bottom-chartLine.Values[i]*scale);
}
g.DrawLines(linePen, Values);
}
private void DrawLegend() {
//Draw Legend Box
ControlPaint.DrawBorder(g, (Rectangle)legendRect, SystemColors.WindowFrame, ButtonBorderStyle.Solid);
LinearGradientBrush b = new LinearGradientBrush(legendRect, backColor, Color.SteelBlue, LinearGradientMode.Horizontal);
r.Inflate(-1, -1);
g.FillRectangle(b, legendRect);
b.Dispose();
float startY = 5;
foreach (ChartLine cl in chartData.Lines) {
Pen p = new Pen(cl.Color) ;
p.Width = p.Width*4;
SizeF textsize = g.MeasureString(cl.Title, legendFont);
float lineY = startY + textsize.Height / 2 ;
g.DrawLine(p, r.X + 7, lineY, r.X + 25, lineY);
g.DrawString(cl.Title, legendFont, new SolidBrush(foreColor), r.X + 30, startY);
startY += (textsize.Height+2);
}
}
}
}


将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
上一篇文章:asp.NET特写
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 《高性能的数据库》 第四..
· 在JSP、ASP和PHP网站网页..
· 微软向公众展示了Live S..
· 比房地产更赚钱 网络病毒..
· 谈谈做站与站长的站德问..
· fireWorks蒙板抠图
· 利用XMLHTTP 从其他页面..
· 全球博客数突破7000万 中..
· 网页素材 线条(线条素材..
· 一定要用好调整层(9)
· Flash游戏制作规划与流程..
· 未公开的Flash MX使用摄..
· 利用FLASH 8绘图功能制作..
· Mysql 4.1 Windows 下升..
· Photoshop打造美女残缺美..
· SQL语句中UNION和UNION ..
相关文章
· 用ASP.NET开发电子商务网站..
· 使用ASP+MSSQL时添加数据无..
· 用Asp隐藏文件路径,实现防盗..
· 用ASP代码得到客户端IP和当..
· 使用ASP记录在线用户的数量..
· ASP实例:用ASP程序实现网站..
· 初学ASP来看:用ASP查看数据..
· ASP实例:用ASP编写更人性化..
· 适合初学者的ASP教程:常用..
· 用ASP调用存储过程返回临时..
· 用ASP批量更新SQL SERVER数..
· 利用Asp.net Ajax异步获取x..
· 使用ASP脚本技术
· 用ASP批量更新SQL SERVER数..
· 应用:用ASP实现在线文章翻..
· 用AspJpeg组件,按宽高比例..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号