Dynamically generating images with asp.net (ext. 1)

Source: Internet
Author: User
asp.net| dynamically dynamic Image Generation with asp.net

Scott Guthrie
January 14, 2001

Level:beginner/intermediate

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

The functionality to generate images and. 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, phot OCD, FlashPIX, WMF, EMF and EXIF. Note This there are no license issues to worry about and any of the file formats; Microsoft implementation of each of the format is license free (including for GIF images).

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

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


' 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 a ASPX page to does this and you'll want to make sure your set the appropriate HTTP ContentType header as we ll, so that's the browser client doesn ' t interpret the page ' content as HTML but rather as an image. You can does this either via setting the Response.ContentType property through code, or via the new "ContentType" attribute That's the can set on the top-level page directive:


<%@ Page language= "VB" contenttype= "Image/jpeg"%>


Note This output caching features of ASP.net work for both content as as a for textual output. As such, if you are are dynamically generating a page, can easily leverage the output cache directive to AVO The ID has to regenerate the "image on" each request. Note ' That ' image generation can be expensive and so the feature is highly recommended. For example, the below directive could is used to output cache the generated image for a second interval:


<%@ Page language= "VB" contenttype= "Image/jpeg"%>
<%@ OutputCache duration= "%>"


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

Feel free to use any of the "code however you want" and "like" and "I" samples, Feel free to post elsewhere as 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:bin\chartgen.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 ETS up a tags to the imagegenerator_vb.aspx page that does the actual image generation work).


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.