Thursday, 1 March 2012

The GDI code for converting the string to image and displaying as a Link in C#


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CsharpImage : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
   string str = string.Empty;
     for (int i = 0; i < 5; i++)
      {
         Bitmap bmp = new Bitmap(74, 16, PixelFormat.Format64bppArgb);
         using (Graphics g = Graphics.FromImage(bmp))
          {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Font font = new Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Pixel);
                g.FillRectangle(Brushes.Transparent, new RectangleF(0, 0, bmp.Width, bmp.Height));
                        g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
                         str = "Sudarsan-" + i ;
                        g.DrawString(str, font, new SolidBrush(Color.Black), 1, 1);
                        bmp.Save(Server.MapPath("~/images/"+i+".jpg"), ImageFormat.Jpeg);
                  }
           }
           
  for (int i = 0; i < 3; i++)
  {
   Response.Write(string.Format("<a href='{0}'><img src='images/{0}.jpg'/></a><br/>", i));
  }
 }
}

No comments:

Post a Comment