• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1151
  • Last Modified:

Create graphics on the fly in ASP.NET

I'm trying to create a button style control (probably derived from the ImageButton control) that will dynamically build a button from 4 graphics: a left side image, a right side image, a middle image that could be stretched or tiled, and a text element that needs to be rendered in a specified font, size, color, etc.

The purpose of this control is to create an ImageButton type object that can be dynamically sized AND contain custom text. The problem is I don't know how to start. Can anyone offer any starting steps, advice, or guides on how I might accomplish this task?
0
Russ Suter
Asked:
Russ Suter
1 Solution
 
mikeuttCommented:
You could create a Generic Handler. Then use the .Net Drawing library to create a image dynamically. Like so.
In the code I posted I take an existing image and add query string text to the image.

http://www.mikeutterback.net\ImageHandler.ashx?text=one
http://www.mikeutterback.net\ImageHandler.ashx?text=two
http://www.mikeutterback.net\ImageHandler.ashx?text=three

Maybe someone who knows the .Net Drawing library better then I could explain how to combine more then one image.
<%@ WebHandler Language="C#" Class="ImageHandler" %>
 
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
 
public class ImageHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        Bitmap image = new Bitmap(@"C:\Inetpub\root\Images\button.jpg");
        Graphics G = Graphics.FromImage(image);
        G.DrawString(context.Request["Text"], new Font("Arial", 3, FontStyle.Regular), Brushes.Black,0, 0);
        context.Response.ContentType = "image/jpeg";
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
 
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
 
}

Open in new window

ImageHandler.jpg
0
 
Russ SuterAuthor Commented:
Thanks for the info. It's a great start but it has two major problems that make it unusable for my purpose.

1. The image quality is badly degraded when it is redrawn.
2. The text isn't centered.

Can you offer any advice on how I may solve either of these two issues?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now