Click here to Skip to main content
11,435,451 members (62,271 online)
Click here to Skip to main content

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

, 23 Mar 2003
Rate this:
Please Sign up or sign in to vote.
This is a simple method to display a custom drawn graphic on the fly without referencing an .aspx page.

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

Share

About the Author

Andrew S Traub
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralLimited to one image Pin
mohaaron17-Aug-06 11:50
membermohaaron17-Aug-06 11:50 
QuestionRendering on the fly in UserControl? Pin
Phoenix 2 You30-Dec-05 12:09
memberPhoenix 2 You30-Dec-05 12:09 
GeneralI got one wich uses custom image control and no .aspx files Pin
Faiz VP9-Nov-05 9:53
memberFaiz VP9-Nov-05 9:53 
GeneralShow images WITHOUT using Response.OutputStream Pin
Rangaraya26-Feb-04 15:20
sussRangaraya26-Feb-04 15:20 
GeneralDemo Pin
arody9-Nov-03 14:44
memberarody9-Nov-03 14:44 
GeneralI'm not convinced.... Pin
Paul B25-Mar-03 4:30
memberPaul B25-Mar-03 4:30 
GeneralRe: I'm not convinced.... Pin
Andrew S Traub19-May-03 9:10
memberAndrew S Traub19-May-03 9:10 
GeneralRe: I'm not convinced.... Pin
gerbenvl28-Dec-03 10:48
membergerbenvl28-Dec-03 10:48 
GeneralRe: I'm not convinced.... Pin
Andrew S Traub29-Dec-03 5:31
memberAndrew S Traub29-Dec-03 5:31 
GeneralRe: I'm not convinced.... Pin
LisaStar11-Apr-05 12:40
memberLisaStar11-Apr-05 12:40 
Generalcant get to work Pin
lanzerp23-Mar-03 21:39
memberlanzerp23-Mar-03 21:39 
GeneralRe: cant get to work Pin
Andrew S Traub19-May-03 9:09
memberAndrew S Traub19-May-03 9:09 
GeneralRe: cant get to work Pin
Shaman25-Nov-03 6:01
memberShaman25-Nov-03 6:01 
GeneralRe: cant get to work Pin
gerbenvl26-Dec-03 12:56
membergerbenvl26-Dec-03 12:56 
GeneralAlternatives and Concerns Pin
Heath Stewart23-Mar-03 19:07
editorHeath Stewart23-Mar-03 19:07 
GeneralHey Andrew Pin
Nick Parker22-Mar-03 5:01
editorNick Parker22-Mar-03 5:01 
GeneralRe: Hey Andrew Pin
Andrew S Traub22-Mar-03 11:01
memberAndrew S Traub22-Mar-03 11:01 
GeneralRe: Hey Andrew Pin
Nick Parker22-Mar-03 12:25
editorNick Parker22-Mar-03 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 24 Mar 2003
Article Copyright 2003 by Andrew S Traub
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid