Blue Theme Orange Theme Green Theme Red Theme
 
HeaderAd
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » GDI+ & Graphics » Generating Images Dynamically in ASP.NET

Generating Images Dynamically in ASP.NET

This article explains how to create images dynamically at run-time in ASP.NET.

Technologies: .NET Compact Framework, .NET 1.0/1.1, ASP.NET 1.0,Visual C# .NET
Total downloads : 288
Total page views :  14061
Rating :
 2.5/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
GenerateImagesDynamically.zip
 
ArticleAd
Become a Sponsor



Introduction

We can create images dynamically very easy in ASP.NET using .Net framework. .Net framework classes have various capabilities of image manipulation.

.Net framework classes offer several classes for image creation and manipulation. These classes are useful for developers to draw various shapes, create text and generate images of any type like Jpeg, Gif, and BMP etc.

This article explains how to create images at run time.

Create a Text Image at Runtime

To create images the namespace used id System.Drawing this namespace contains two classes System.Drawing.Bitmap and System.Drawing.Graphics, useful for us at present. 

To demonstrate simple dynamic ASP.NET image generation, we'll create an ASP.NET Web page with two dropdown boxes, one text box, and a button. The first dropdown box allows us to choose the background color of our image, while the second allows us to select a font. The text box will receive the text that will be displayed on the image, and the button will generate the image.

The first in creating dynamically generated image is to create a new instance of  Bitmap object, and to specify the width and height of the bitmap we are generating.

Bitmap    oBmp1  = new Bitmap(460,60);
 
The next step in our process is to create a new Graphics object whch will allow us to draw on our bitmap.

Graphics  oGrp1  = Graphics.FromImage(oBmp1);

We then declare a structure of type System.Drawing.Color, which will define the background color of our dynamic image.

System.Drawing.Color ocolor  = new Color();

Now we need to get the values given by the user in Text Box and the two Dropdown lists.

string sColor =  DropDownList1.SelectedItem.Text;
string sText =   TextBox1.Text;
string sFont =   DropDownList2.SelectedItem.Text;

Now we have to set oColor  depending on sColor.

if(sColor == "Yellow")

{

    ocolor = Color.Yellow;

}

else if(sColor == "Blue")

{

    ocolor = Color.Blue;

}

else if(sColor  == "Green")

{

    ocolor = Color.Green;

}

Next, we create two brushes, which will help us to draw our image. The first one, oBrush, will be used to draw the background of the image:

SolidBrush oBrush = new SolidBrush(ocolor);

The second brush will be used to draw the text entered by the user; we hardcode the brush color as white.

SolidBrush oBrushWrite = new SolidBrush(Color.White);

We're going to use the first brush to fill the image rectangle, because the default color of new bitmaps is black. We call the FillRectangle method of oGraphic to paint the background in the color selected by the user (oColor):

oGrp1.FillRectangle(oBrush,0,0,500,75);

So far, we've changed the background color of our image, but we haven't written any text. We'll use the DrawString method of the oGraphic object to do that. The DrawString method has several overloads. This is the one we're going to use:

DrawString(String,Font,Brush,Fpoint);

The first parameter is just the string that we want to write; the second parameter defines the font to be used; the third parameter defines a brush (this will be the second brush we created above); the last parameter defines a drawing starting point relative to the top-left corner of our image. Before we call the DrawString method, however, we need to create an instance of the Font class, and FPoint structure that defines the point at which drawing should start.

The user chose a font for the text, so we pass sFont as the first parameter, and hardcode the second parameter, which defines the font size:

Font    oFont  = new Font(sFont,13);
PointF  oPoint = new PointF(5F,5F);

so now we created instances of Font and PointF and now DrawString parameters are ready:

oGrp1.DrawString(sText,oFont,oBrushWrite,oPoint);

Now the last thing is we have to send the image to browser for that.

Response.ContentType = "image/jpeg" ;
oBmp1.Save (Response.OutputStream, ImageFormat.Jpeg);

That's all we generated a Text image dynamically at run time.

The below image shows the sample output:

output

Figure 1.

Summary:

This article explains how to create simple images dynamically at runtime.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Rajendra Kumar Yerra
Currently Working in Wipro Technologies, Hyderabad.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
GenerateImagesDynamically.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
a bit on the trivial sidesimon7/12/2007
How about embedding a generated image into a useful page, and doing it without using a temporary file?
Reply | Email | Delete | Modify | 
hirathnakar8/15/2007
i want javascript for creating msgbox in asp.net
Reply | Email | Delete | Modify | 
How to set the created image to image boxHema1/6/2009
after creating the image dynamically, how to set the same to the image box, with our saving them anywhere?
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved