asp.net| Dynamic | Graph in asp.net dynamic generation
. NET has a neat feature of writing code to generate dynamic graphics, and then saving the generated graphics on disk or sending it directly to the client's browser.
Principle
The features of. NET generated graphics are encapsulated in the System.Drawing namespace, which supports a variety of graphics file formats, including: JPEG, GIF, PNG, TIFF, BMP, PhotoCD, FlashPIX, WMF, EMF, and EXIF.
The common mechanism for creating these graphs is to construct a bitmap (bitmap) object that provides the memory space for the graphic to be created. You can then call its "save" method, save it on disk, or send it to any. NET in the output stream. Asp. NET reveals the. NET output stream through the Response.outputstream property, which means you can send the contents of the graphic directly to the browser without saving it to disk.
For example, in VB, you can write code like this:
' Create in-memory BitMap of JPEG
Dim Mychartengine as New chartengine
Dim Stockbitmap as BitMap = Mychartengine.drawchart (+, Mychartdata)
' Render BitMap Stream back to Browser
Stockbitmap.save (Response.outputstream, Imageformat.jpeg)
If you are using an ASPX page to do this, you should also set the appropriate HTTP ContentType (content Type) header file so that the browser does not interpret the content of the page as HTML, but instead interprets it as a graphic. You can do this by setting the Response.ContentType property in code, or by setting a new "ContentType" property on the top-level page directive:
Note: The ASP.net output caching function works for both text content and binary output. This way, if you are dynamically generating graphics from a page, you can easily use output-caching instructions to avoid rebuilding graphics on each request. Note that graphics generation can be cumbersome, so I highly recommend that you use this feature. For example, the generated graphics can be cached for output every 60 seconds with the following instructions:
In order to provide you with a complete example of generating graphics, here is a simple stock graph to generate sample code. Please note that the price of the stock is not real, but I imagined it. The sample code uses a custom "Chartengine" class that encapsulates the logic required to build a normal graphic. You should be able to use this component to make any custom graphics that are not limited to stock data.
In the sample code, whatever you want to use, you can use it, or you can paste it anywhere in the article, and in other samples.
To run this sample, save the following to a file placed under an application in IIS VRoot: see source code.
Then, type the following statement at the command line:
MkDir bin
Csc/t:library/out:bin\chartgen.dll CHARTENGINE.CS/R:SYSTEM.WEB.DLL/R:SYSTEM.WINFORMS.DLL/R:SYSTEM.DRAWING.DLL/R : System.dll
After Chartengine is compiled, click on the Stockpicker.aspx page to run this sample. In this way, a tag that points to the Imagegenerator_vb.aspx page is set up, which is to do the actual graphics generation work.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.