Monday, February 27, 2006

Dynamic String Image in .NET

On one of the blogs I was looking today on msdn, There was this code on how to dynamically generate an image in ASP.NET. I modified this to show a string in that image.

---------------------------------------------

private void Page_Load(object sender, EventArgs e)
{
// Set the page's content type to JPEG files
// and clear all response headers.
Response.ContentType = "image/jpeg";
Response.Clear();
// ...
// Create integer variables.
int height = 50;
int width = 100;
// ...
// Create a bitmap and use it to create a
// Graphics object.
using (Bitmap bmp = new Bitmap(
width, height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
// ...
Random random = new Random();
string randomString = random.Next().ToString();
Font drawFont = new Font("Arial", 11);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
float x = 70.0F;
float y = 20.0F;
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
// Draw string to screen.
g.Clear(Color.AliceBlue);
g.DrawString("Apple", drawFont, drawBrush, x, y, drawFormat);
// ...
}
// Save the bitmap to the response stream and
// convert it to JPEG format.
bmp.Save("C:\\Test.jpeg", ImageFormat.Jpeg);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}

-----------------------------------------------------------

0 Comments:

Post a Comment

<< Home