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:

 


Comments (13)
  1. David Kyle says:

    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

  2. Jeremy Rule says:

    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.

  3. ShadowChaser says:

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

  4. Anonymous says:

    Christmas in August.

  5. DrPizza says:

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

    That’s bad luck, then, ‘cos they’ve been cut.

  6. Adam Hill says:

    And now MS has removed Dynamic Image generation from 2.0.

    Most annoying.

  7. diceboy says:

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

    " I "

    " LIKE "

    " ASP "

Comments are closed.

Skip to main content