ASP.NET dynamically created image
Http://www.tongyi.net: ChinaASP Click: 435
Level: Beginner / IntermediateOne 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. Because ASP.NET exposes a .NET OutputStream via stream the Respons . E.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 you set the appropriate http contenttype header as well, so what 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: <% @ 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 could be used to output cache the generated image for a 60 second interface: <% @ page language = "vb" contenttype = "image / jpeg"%> <% @ outputcache duration = "60"%> for a completion, i've increte, 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 Bincsc / 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 (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: