Dynamically generate images in ASP. NET

Source: Internet
Author: User

. NET has a very clever feature, that is, it can write code to generate dynamic graphics, and then save the generated graphics on the disk or directly send it to the client browser.

Principle

Use. NET Graphics generation function is encapsulated in System. the Drawing namespace supports multiple graphic file formats, including JPEG, GIF, PNG, TIFF, BMP, PhotoCD, FlashPIX, WMF, EMF, and EXIF.

The general mechanism for creating these images is to construct a BitMap object, which provides memory space for the image to be created. Then, you can call its "Save" method to Save it on a disk or send it to any. NET output stream. ASP. NET uses the Response. OutputStream attribute to reveal the. NET output stream. This means that you can directly send the image content to the browser without saving it to the disk.

For example, you can write such code in VB:

"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 use An ASPX page to achieve this purpose, you need to set an appropriate HTTP ContentType content type) header file at the same time, so that the browser will not interpret the page content as html, but as a graph. You can use the code to set the Response. ContentType attribute, or set the new "ContentType" attribute on the top-level page command to achieve this:

Note: The output cache function of ASP. NET works for both text content and binary output. In this way, if you are dynamically generating a graph from a page, you can easily use the output cache command to avoid re-generating the graph on each request. It may be difficult to generate images. Therefore, I strongly recommend that you use this function. For example, you can use the following command to cache the output of the generated image every 60 seconds:

Stock graphics generation routine
To provide you with a complete example of generating images, a simple sample code for generating stock images is included here. Please note that the stock price is not real, just as I imagined. The sample code uses a custom "ChartEngine" class that encapsulates the logic required to create a common image. You should be able to use this component to create any custom image. It is not limited to stock data.


In the sample code, you can use whatever you want, or paste it anywhere in the article, or in other samples. To run this sample, save the following content as a file and put it under the application Vroot of IIS: see the source code.
Then, type the following statement in 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 the StockPicker. aspx page to run the sample. In this way, a tag pointing to the ImageGenerator_VB.aspx page is set to generate the actual image.


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.