Use ASP. NET to dynamically generate images (to 1)

Source: Internet
Author: User
Tags stock prices

Dynamic Image Generation with ASP. NET

Scott Guthrie
January 14,200 1

Level: Beginner/Intermediate

One of the neat features that you can now leverage. 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. 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. 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 wowould 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 doesnt interpret the pages 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:


<% @ Page Language = "VB" ContentType = "image/jpeg" %>


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 cocould be used to output cache the generated image for a 60 second interval:


<% @ Page Language = "VB" ContentType = "image/jpeg" %>
<% @ OutputCache Duration = "60" %>


For a complete sample of how to use image generation, Ive defined ded a simple stock chart generation sample below. note that the stock prices arent 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 shoshould 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 ).


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.