Welcome to MSDN Blogs Sign in | Join | Help

Rule's Roost

Jeremy Rule's blog

Syndication

Dynamic Image Generation with ASP.Net

I've was playing around with image generation. Below is a picture of Josh who is a very interesting guy. His picture worked quite well because it was a clean slate to write text on.

The HTML tag <img src="http://www.rulesroost.com/josh.aspx?Text=Got%20Milk?"> produces:

 

And <img src="http://www.rulesroost.com/josh.aspx?Text=This%20is%20a%20much%20longer%20sentence."> shows:

 
And here is the code for the aspx file:

1<%@ OutputCache Duration="500" VaryByParam="Text" %>
2<%@ Page Language="C#" trace="false" Explicit="true" aspcompat="true" Debug="true" %>
3<%@ Import Namespace="System" %>
4<%@ Import Namespace="System.IO" %>
5<%@ Import Namespace="System.Text" %>
6<%@ Import Namespace="System.Drawing" %>
7<%@ Import Namespace="System.Drawing.Imaging" %>
8<%@ Import Namespace="System.Drawing.Text" %>
9<%@ Import Namespace="System.Drawing.Drawing2D" %>
10
11<script runat="server">
12 private void Page_Load(object sender, System.EventArgs e)
13 {
14 Bitmap bitmap = new Bitmap(Server.MapPath("josh.bmp"));
15 MemoryStream memStream = new MemoryStream();
16
17 // generate image
18
19 // Create a graphics object for drawing.
20 Graphics g = Graphics.FromImage(bitmap);
21 g.SmoothingMode = SmoothingMode.AntiAlias;
22
23 int width = bitmap.Width;
24 int height = bitmap.Height;
25
26 string familyName = "Tahoma";
27 string text = Request.Params["Text"];
28
29 // get a rectangle on his shirt
30 Rectangle rect = new Rectangle(150, 216, 210, 135);
31
32 // Set up the text font.
33 Font font;
34 font = new Font(familyName, 16F, FontStyle.Regular);
35
36 // Set up the text format.
37 StringFormat format = new StringFormat();
38 format.Alignment = StringAlignment.Center;
39 format.LineAlignment = StringAlignment.Center;
40
41 // Create a path using the text and warp it to fit over his contour
42 GraphicsPath path = new GraphicsPath();
43 path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format);
44
45 PointF[] points =
46 {
47 new PointF(rect.X - 10, rect.Y - 8),
48 new PointF(rect.X + rect.Width - 20, rect.Y + 4),
49 new PointF(rect.X - 8, rect.Y + rect.Height - 15),
50 new PointF(rect.X + rect.Width - 10, rect.Y + rect.Height + 4)
51 };
52 Matrix matrix = new Matrix();
53 matrix.Translate(0F, 0F);
54 path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
55
56 // Draw the text.
57 HatchBrush hatchBrush = new HatchBrush(
58 HatchStyle.LargeConfetti,
59 Color.LightGray,
60 Color.DarkGray);
61
62 g.FillPath(hatchBrush, path);
63
64 Response.Clear();
65 Response.ContentType="image/jpeg";
66 bitmap.Save(memStream, ImageFormat.Jpeg);
67 memStream.WriteTo(Response.OutputStream);
68
69 // Clean up.
70 font.Dispose();
71 hatchBrush.Dispose();
72 g.Dispose();
73 bitmap.Dispose();
74
75 }
76
77</script>
78
 
Notice the Reponse.ContentType="image/jpeg" - that causes the output to be a picture and not HTML. 
Also notice the word-wrap is done for me with the StringFormat object.
Other resources:

 

Published Monday, August 16, 2004 4:11 PM by jrule

Comments

# re: Dynamic Image Generation with ASP.Net @ Monday, August 16, 2004 4:39 PM

The image appears to be pretty good in quality. Did you have to deal with the problems addressed in the following article:

http://msdn.microsoft.com/library/en-us/dnaspp/html/colorquant.asp?frame=true

If not, how did you get around them?

david

David Kyle

# re: Dynamic Image Generation with ASP.Net @ Monday, August 16, 2004 4:43 PM

Wow that article talks about fancier stuff than I even had in mind. I wonder if, because I am loading from a 24-bit BMP, and not a JPEG or GIF I don't experience the image quality issue. I'll have to read that whole article and try to understand it.

Jeremy Rule

# re: Dynamic Image Generation with ASP.Net @ Monday, August 16, 2004 6:58 PM

I can't wait until ASP.NET 2.0.. with ASIX files!

ShadowChaser

# Features cut in ASP.NET 2.0 @ Tuesday, August 17, 2004 4:26 AM

Scott McCulloch

# Scobleizer Trackback! @ Tuesday, August 17, 2004 8:11 AM

Christmas in August.

TrackBack

# Dynamic Images In ASP.NET 2.0 @ Tuesday, August 17, 2004 3:31 PM

Deep Thoughts...

# re: Dynamic Image Generation with ASP.Net @ Tuesday, August 17, 2004 3:41 PM

"I can't wait until ASP.NET 2.0.. with ASIX files! "
That's bad luck, then, 'cos they've been cut.

DrPizza

# re: Dynamic Image Generation with ASP.Net @ Friday, August 20, 2004 10:21 AM

And now MS has removed Dynamic Image generation from 2.0.

Most annoying.

Adam Hill

# re: How to protect your web site pictures from being saved @ Thursday, September 02, 2004 7:11 AM

Calvin Hsia's WebLog

# re: Dynamic Image Generation with ASP.Net @ Thursday, September 09, 2004 7:31 PM

could you use this method to do multiline stuff by recognising something like carriage returns. I want to do something like
" I "
" LIKE "
" ASP "

diceboy

# Feed Search Engine - All Fresh Articles And News Are Here @ Sunday, November 25, 2007 11:11 AM

PingBack from http://feeds.maxblog.eu/item_1144962.html

Feed Search Engine - All Fresh Articles And News Are Here

# &amp;c. &raquo; Blog Archive &raquo; To design a panel. @ Thursday, April 24, 2008 10:31 AM

PingBack from http://tommysetiawan.com/blog/?p=8

&amp;c. &raquo; Blog Archive &raquo; To design a panel.

# Web Development Tips, Tricks &amp; Trivia &raquo; Blog Archive &raquo; HOW TO generate an image dynamically with ASP.Net @ Wednesday, May 27, 2009 7:40 AM

PingBack from http://webdevelopment.mobiforumz.com/2005/06/16/how-to-generate-an-image-dynamically-with-aspnet/

Web Development Tips, Tricks &amp; Trivia &raquo; Blog Archive &raquo; HOW TO generate an image dynamically with ASP.Net

New Comments to this post are disabled
Page view tracker