Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Generating ASP.NET Images on the Fly | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Generating ASP.NET Images on the Fly

Generating ASP.NET Images on the Fly








Article Posted On Date : Wednesday, May 25, 2011


Generating ASP.NET Images on the Fly
Advertisements

ASP.NET and the .NET framework make the task of generating images on the fly very easy. With classic ASP, developers were forced to use third-party components like ASPImage and ASPPicture for dynamic image manipulation. Fortunately, with ASP.NET, those days are over. Various image manipulation capabilities are now built directly into the .NET framework classes.

The .NET Framework offers several classes for image generation and manipulation. ASP.NET developers can use these classes to draw various shapes, create text, and generate images in many different formats, including GIF, JPEG, PNG, etc.

Now, for the inevitable question: why would you need to generate images on the fly for your ASP.NET Web pages? Here are several practical applications of dynamic image generation:

- Generate a thumbnail gallery from your full-size gallery images.
- Create dynamic charts based on dynamic user input or database data.
- Generate images with random text (access code) for use with your user registration pages to prevent automated registrations.

Create a Text Image on the Fly

Let's use two .NET classes located in the System.Drawing namespace to illustrate how to create images on the fly with ASP.NET. The two framework classes are System.Drawing.Bitmap and System.Drawing.Graphics.

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. Here's the code that displays these form elements:

<form runat="server">
<asp:TextBox runat="server" id="Text" />

<asp:dropdownlist runat="server" id="BackgroundColor">
<asp:ListItem Value="red">Red</asp:ListItem>
<asp:ListItem Value="green">Green</asp:ListItem>
<asp:ListItem Value="navy">Navy</asp:ListItem>
<asp:ListItem Value="orange">Orange</asp:ListItem>
</asp:dropdownlist>

<asp:dropdownlist runat="server" id="Font">
<asp:ListItem Value="Arial">Arial</asp:ListItem>
<asp:ListItem Value="Verdana">Verdana</asp:ListItem>
<asp:ListItem Value="Courier">Courier</asp:ListItem>
<asp:ListItem Value="Times New Roman">Times New Roman</asp:ListItem>
</asp:dropdownlist>

<asp:Button runat="Server" id="SubmitButton" Text="Generate Image" />

</form>

Every time you request an ASP.NET page, the Page_Load event is triggered on the server and the Page_Load sub-routine is called. Within this sub-routine, you can check to see whether the request for the page was generated via a data postback (the user clicked on the submit button, for example), whether the user came to this page from another page, or whether the user simply typed the page URL into the browser's address bar.

In this example, it's important to know if the page was posted back or not, because we want to generate a dynamic image only when the user clicks on the submit button -- not when he of she first loads the page. The Page.IsPostBack property is true if the APS.NET page is loaded in response to a client postback; otherwise, it's false. So the first thing we do within the Page_Load sub-routine is check the value of Page.IsPostBack:

If Page.IsPostBack Then

If the value is false, we don't do anything and the page merely displays the ASP.NET controls we declared above. If the value is true, we must generate an image. Here's how it's done.

The first step in creating our dynamically generated image is to create a new Bitmap object, and to specify the width and the height of the image we're generating:

Dim oBitmap As Bitmap = New Bitmap(468, 60)

The next step is to create a new Graphics object, which will allow us to draw on our bitmap:

Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)

We then declare a structure of type System.Drawing.Color (represents an RGB color), which will define the background color of our dynamic image:

Dim oColor As System.Drawing.Color

Now, we need to get the values that were posted back to the page from the two dropdown boxes and the text box:

Dim sColor As String = Request("BackgroundColor")
Dim sText As String = Request("Text")
Dim sFont As String = Request("Font")

We run a Select Case statement to set the value of oColor (the background color of our image), depending on the sColor value:

Select Case sColor
Case "red"
oColor = Color.Red
Case "green"
oColor = Color.Green
Case "navy"
oColor = Color.Navy
Case "orange"
oColor = Color.Orange
Case Else
oColor = Color.Gray
End Select

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:

Dim oBrush As New SolidBrush(oColor)

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

Dim oBrushWrite As 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):

oGraphic.FillRectangle(oBrush, 0, 0, 468, 60)

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:

Dim oFont As New Font(sFont, 13)

Then, we define a structure of type PointF, which represents an ordered pair of X and Y coordinates that define a point in a two-dimensional plane. Again, we just hardcode the drawing start point for the text, because this is not configurable in our example:

Dim oPoint As New PointF(5F, 5F)

We can now call the DrawString method and draw our text:

oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)

The last thing that's left to do is to save the newly created image on the server:

oBitmap.Save(Server.MapPath("generated_image.jpg"), ImageFormat.Jpeg)

This code saves the image in JPEG format, but you can save it as GIF, TIFF, PNG, or any other popular image format.

Finally, we display a link to our new image:

Response.Write("View the generated image <a target=""_blank"" href=""generated_image.jpg"">here</a>")

If you don't want to save the image on the server file system, you can send it directly to the browser using the following code:

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

In fact, you can house the code that generates the dynamic image in a separate ASP.NET page, and reference this page from other ASP.NET pages like this:

<img src="image_generate.aspx">

Of course, your image_generate.aspx page has to have the proper image ContentType, for example:

Response.ContentType = "image/jpeg"

Or:

Response.ContentType = "image/gif"

You can find the full source code of this page at the end of this article.

Create a Thumbnail on the Fly

Let's now look at images as we step through the process involved in dynamically creating thumbnails in ASP.NET.

First, we'll create an ASP.NET page called thumbnail.aspx and import the System.Drawing and System.Drawing.Imaging classes:

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

The thumbnail.aspx page will get 3 parameters in its query string:


image -- the file path of the image file that will be resized to a thumbnail
w -- the desired width of the thumbnail
h -- the desired height of the thumbnail

In the Page_Load sub-routine, we declare 2 System.Drawing.Image objects -- oImage and oThumbnail. Then we get the absolute path of the image file with the following line:

sImagePath = Server.MapPath(Request.QueryString("image"))

Note: be aware of the security implications here. By passing a tricky image parameter in the query string, a hacker could persuade the script to attempt to access and display any file on the server, whether it's part of your Website or not. To guard against this, you'd want to ensure that your file system permissions were correctly configured; also, some validation logic for the parameter would not go astray in this script. For the purposes of this article, however, we'll stick with this na�ve approach.

On the next step, we actually create the oImage object using the FromFile method and passing the absolute file path we got in the previous step.

oImage = oImage.FromFile(sImagePath)

Then we get the desired thumbnail width and height:

iWidth = Request.QueryString("w")
iHeight = Request.QueryString("h")

The next step is to use the GetThumbnailImage method, which gets a thumbnail image from an Image object:

oThumbnail = oImage.GetThumbnailImage(iWidth, iHeight, Nothing, Nothing)

We pass the width and the height of the thumbnail as the first two parameters. For the purpose of this article, it's enough to pass Nothing as third and fourth parameters of the GetThumbnailImage method, without any further explanation.

Finally, as in the previous example, we directly send the image to the browser using the following code:

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

It was that easy!






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2025 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2025

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2025 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.