Dymanic text with center alignment

I want to create dynamic text using GDI+ and want to center align text with in given rectangular, Is is possible?

Check following code which creates dynamic image with center aligned text.

Run sample code


<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">
    void Page_Load(Object sender,EventArgs e){
        Bitmap bmp = new Bitmap(300,50);
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.White);
        // Center Align 
        Rectangle r = new Rectangle(0,0,299,49);
        StringFormat f = new StringFormat();
        // Change this to change horizontal alignment.
        f.Alignment = System.Drawing.StringAlignment.Center; 
        // Change this to chenge verticle alignment.
        f.LineAlignment = System.Drawing.StringAlignment.Center;
        g.DrawRectangle(Pens.Black,r);
        g.DrawString("CenterAlign",
            new Font("Verdana",12),Brushes.Black,r,f);
        Response.ContentType = "image/gif";
        bmp.Save(Response.OutputStream,
            System.Drawing.Imaging.ImageFormat.Gif);
    }
</script>
Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.

Categories