ASP.NET动态创建图象 (转)

本文介绍如何使用ASP.NET和System.Drawing命名空间轻松生成动态图象,并直接将其流传输到浏览器客户端。文中提供了完整的示例代码,展示了如何创建图表并进行自定义。
ASP.NET动态创建图象 (转)[@more@] ASP.NET动态创建图象 
http://www.tongyi.net 出处:chinaasp 点击:435 

Level: Beginner/Intermediate

One of the neat features that you can now leverage with .NET is the ability to easily generate dynamic 
images from code, which you can then either save to disk or directly stream back to a browser client with ASP.net

The functionality to generate images with .NET is encapsulated within the System.Drawing namespace. It provides
 built-in support for generating images with a numer of file formats including: JPEG, GIF, PNG, TIFF, BMP, 
PhotoCD, FlashPIX, WMF, EMF and EXIF. Note that there are no license issues to worry about with any of 
these file formats; microsoft implementation of each format is license free (including for GIF images).

The general mechanism through which you generate these graphics images is by constructing a BitMap object
 which provides an in-memory representation of your image. You can then call its "Save" method to 
either save it to disk, or stream it out to any .NET output stream. Because ASP.NET exposes a .NET 
OutputStream via the Response.OutputStream property. This means you can stream the image contents
 directly to the browser without ever having to save it to disk.

For example, to do this in VB you would write code like: 


 ' 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 Browser
 StockBitMap.Save(Response.OutputStream, ImageFormat.JPEG) 


If you are using an aspx page to do this, you will want to make sure you set the appropriate HTTP ContentType 
header as well, so that the browser client doesn't interpret the page's content as html but rather as an 
image. You can do this either via setting the Response.ContentType property through code, or via the new 
"ContentType" attribute that you can set on the top-level page directive: 


 


Note that the output caching features of ASP.NET work for both textual content as well as for binary output.
 As such, if you are dynamically generating an image from a page, you can easily leverage the output cache 
directive to avoid having to regenerate the image on each request. Note that image generation can be 
expensive, so this feature is highly recommended. For example, the below directive could be used to 
output cache the generated image for a 60 second interval: 



 


For a complete sample of how to use image generation, I've included a simple stock chart generation sample
 below. Note that the stock prices aren't real, just wishful thinking on my part. The sample uses a custom 
"ChartEngine" class that helps encapsulate the logic required to build up a generic chart. You should be 
able to use this helper component to do any custom charting of your own, it is definitely not limited 
to just stock data. 

Feel free to use any of the code however you want and like with all my other samples, feel free to post
 elsewhere as well as to use for articles, other samples, etc). 

 


Instructions:


To run the sample, copy/paste/save the below files into an IIS Application Vroot. Then type the 
below statements into a command line: 


mkdir bin
csc /t:library /out:binchartgen.dll 
ChartEngine.cs /r:System.web.dll /r:System.WinForms.dll /r:System.Drawing.dll /r:System.dll 


Once the chartengine helper utility
 is compiled, hit the StockPicker.aspx page to run the sample (note that this in turn sets up a  
tag to point to the ImageGenerator_VB.aspx page that does the actual image generation work). 


StockPicker.aspx:


 


 
 

SCOtt's Stock Picker


 
 M sfT
 SUN
 
 
 

 
 
 


ImageGenerator_VB.aspx:








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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-989614/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-989614/

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值