Key Takeaways
- ASP.NET and the .NET framework simplify the process of generating images on the fly, eliminating the need for third-party components like ASPImage and ASPPicture. This is achieved through various image manipulation capabilities built directly into the .NET framework classes.
- Dynamic image generation in ASP.NET can be used to create a thumbnail gallery from full-size images, generate dynamic charts based on user input or database data, and create images with random text for user registration pages to prevent automated registrations.
- The process of generating images on the fly involves creating a new Bitmap object and using the Graphics class to draw on the Bitmap. The image can then be saved in various formats such as JPEG, GIF, PNG, etc. The Bitmap and Graphics classes are part of the System.Drawing namespace.
- While generating images on the fly provides many benefits such as creating dynamic content and saving server space, it can also be resource-intensive, especially for complex images or high-traffic websites. It requires a good understanding of the Bitmap and Graphics classes and may need optimization to improve performance.
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" />
<br><br>
<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>
<br><br>
<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 thumbnailw
— the desired width of the thumbnailh
— 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 naive 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!
The fully functional VB.NET source-code of both examples can be downloaded here.
Frequently Asked Questions (FAQs) about Generating ASP.NET Images on the Fly
How can I generate images dynamically in ASP.NET?
Generating images dynamically in ASP.NET involves creating a new instance of the Bitmap class and using the Graphics class to draw on the Bitmap. You can then save the Bitmap as an image file. This process allows you to create images on the fly, which can be useful for creating dynamic content for your website.
What are the benefits of generating images on the fly in ASP.NET?
Generating images on the fly in ASP.NET can provide several benefits. It allows you to create dynamic content for your website, which can improve user engagement and increase the overall user experience. It also allows you to save server space, as you don’t need to store pre-generated images.
Can I use ASPImage to generate images on the fly?
Yes, ASPImage is a popular component that allows you to create, edit, and manipulate images in ASP.NET. It provides a wide range of features, including the ability to draw text, shapes, and images, apply filters and effects, and save images in various formats.
How can I display an image in an ASPImage?
To display an image in an ASPImage, you need to set the ImageUrl property of the ASPImage to the URL of the image you want to display. You can also set the AlternateText property to provide a text description of the image for users who can’t see the image.
What is the difference between ASPImage and the Bitmap class in ASP.NET?
ASPImage is a component that provides a high-level interface for creating, editing, and manipulating images in ASP.NET. The Bitmap class, on the other hand, is a lower-level class that provides more direct control over the image data. While ASPImage is easier to use, the Bitmap class provides more flexibility and control.
How can I use the Graphics class to draw on a Bitmap in ASP.NET?
To draw on a Bitmap in ASP.NET, you need to create a new instance of the Graphics class from the Bitmap using the Graphics.FromImage method. You can then use the various methods of the Graphics class to draw on the Bitmap, such as DrawString to draw text, DrawImage to draw an image, and DrawRectangle to draw a rectangle.
Can I save a Bitmap as an image file in ASP.NET?
Yes, you can save a Bitmap as an image file in ASP.NET using the Save method of the Bitmap class. This method allows you to specify the file name and format of the image file, such as JPEG, PNG, or GIF.
What are the limitations of generating images on the fly in ASP.NET?
While generating images on the fly in ASP.NET provides many benefits, it also has some limitations. It can be more resource-intensive than using pre-generated images, especially for complex images or high-traffic websites. It also requires more coding and understanding of the Bitmap and Graphics classes.
Can I use ASP.NET to generate images for a mobile website?
Yes, you can use ASP.NET to generate images for a mobile website. However, you need to consider the different screen sizes and resolutions of mobile devices. You may need to generate different versions of the same image for different devices, or use responsive design techniques to ensure that the images display correctly on all devices.
How can I improve the performance of generating images on the fly in ASP.NET?
There are several ways to improve the performance of generating images on the fly in ASP.NET. You can use caching to store generated images and reuse them, rather than generating them every time they are requested. You can also optimize your code to reduce the amount of processing required to generate the images.
Peter runs a Windows hosting company www.ASP-Hosting.ca, which offers affordable ASP and ASP.NET hosting. He started recently www.ASPdev.org featuring ASP and ASP.NET articles and tutorials.