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?
LVL 21
Russ SuterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.