5,445,109 members and growing! (15,382 online)
Email Password   helpLost your password?
Web Development » Charts, Graphs and Images » Images and multimedia     Intermediate

Graphics on the Fly Directly from ASP.NET Server Control without .ASPX page

By Andrew S Traub

This is a simple method to display a custom drawn graphic on the fly without referencing an .aspx page.
C#, Windows, .NET 1.0, .NET, Visual Studio, ASP.NET, Dev

Posted: 20 Mar 2003
Updated: 23 Mar 2003
Views: 92,743
Bookmarked: 34 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 2.83 Rating: 2.83 out of 5
5 votes, 50.0%
1
2 votes, 20.0%
2
2 votes, 20.0%
3
0 votes, 0.0%
4
1 vote, 10.0%
5

Sample Image - print.png

Introduction

This is a simple article to demonstrate rendering a graphic image on the fly from within a server control without using an .aspx page. One caveat, I have not gotten this method to work at design time, only run time.

On an .aspx page, you would normally create a bitmap with GDI+, and then use this syntax to draw it:

Response.ContentType = "image/jpeg";
objBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

The only examples I had seen which created graphics on the fly (buttons and charts) all included an .aspx page which they would call from the Render method with a query string. The .aspx page would then render the graphic based on the query string.

Here is a way to render the graphic at runtime without the .aspx page.

using System.Drawing;
using System.Drawing.Imaging;
//... 

protected override void Render(HtmlTextWriter output) 
{ 
  Bitmap objBitmap = new Bitmap(120,30); 
  Graphics objGraphics = Graphics.FromImage(objBitmap);
  //Fill the background 

  objGraphics.FillRectangle(new SolidBrush(Color.LightBlue),0,0,120,30); 
  //Create blue-yellow bullet point 

  objGraphics.FillEllipse(new SolidBrush(Color.Blue),3,9,10,10); 
  objGraphics.FillEllipse(new SolidBrush(Color.Yellow),4,10,8,8); 
  //Draw text next to bullet point 

  objGraphics.DrawString("Submit", new Font("Tahoma",8), 
                     new SolidBrush(Color.Green), 16,8); 
  //Render 

  Page.Response.Clear(); 
  Page.Response.ContentType = "image/jpeg"; 
  objBitmap.Save(Page.Response.OutputStream, 
      System.Drawing.Imaging.ImageFormat.Jpeg); 
  //Tidy up 

  objGraphics.Dispose(); 
  objBitmap.Dispose(); 
}

I am not sure of the implications of writing directly to the Page class from within the Render method, but this works at runtime. If anyone knows of a method that works at design time, please let me know.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Andrew S Traub



Occupation: Web Developer
Location: United States United States

Other popular Charts, Graphs and Images articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralLimited to one imagemembermohaaron11:50 17 Aug '06  
GeneralRendering on the fly in UserControl?memberPhoenix 2 You12:09 30 Dec '05  
GeneralI got one wich uses custom image control and no .aspx filesmemberFaiz VP9:53 9 Nov '05  
GeneralShow images WITHOUT using Response.OutputStreamsussRangaraya15:20 26 Feb '04  
GeneralDemomemberarody14:44 9 Nov '03  
GeneralI'm not convinced....memberPaul B4:30 25 Mar '03  
GeneralRe: I'm not convinced....memberAndrew S Traub9:10 19 May '03  
GeneralRe: I'm not convinced....membergerbenvl10:48 28 Dec '03  
GeneralRe: I'm not convinced....memberAndrew S Traub5:31 29 Dec '03  
GeneralRe: I'm not convinced....memberLisaStar12:40 11 Apr '05  
Generalcant get to workmemberlanzerp21:39 23 Mar '03  
GeneralRe: cant get to workmemberAndrew S Traub9:09 19 May '03  
GeneralRe: cant get to workmemberShaman6:01 25 Nov '03  
GeneralRe: cant get to workmembergerbenvl12:56 26 Dec '03  
GeneralAlternatives and ConcernseditorHeath Stewart19:07 23 Mar '03  
GeneralHey AndreweditorNick Parker5:01 22 Mar '03  
GeneralRe: Hey AndrewmemberAndrew S Traub11:01 22 Mar '03  
GeneralRe: Hey AndreweditorNick Parker12:25 22 Mar '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2003
Editor: Smitha Vijayan
Copyright 2003 by Andrew S Traub
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project