Render Image from Font

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I have a custom TrueType .ttf font in the filesystem (not installed)
on the web server.

I want to take the letter "A" from this font, render it as an image,
and return that as a byte stream in a web page.

How can I do that?


Thanks.
 
See System.Drawing. You can do a "usual" Windows graphics app and then
stream the bitmap to the browser...

Patrice
 
Why not just create image files (.png, .jpg, .gif) of each letter.
Then just reference your letter with an <IMG> tag.

<IMG src='letterCapA.png'> Good Example
 
Hi Localhost,

As for your question, I think the GDI+ components under the System.Drawing
/System.Drawing.Text namespace can finish the task. Also, w can use the
"PrivateFontCollection" class to load a (true type font) ttf file from disk
into memory and create a tempory font instance so as to use it(without
installed in the system's font library), here is a simple code snippet.
Hope helps:


=====================================
private void Page_Load(object sender, System.EventArgs e)
{
try
{
System.Drawing.Text.PrivateFontCollection pfc = new
PrivateFontCollection();
pfc.AddFontFile(Server.MapPath("DarkGardenMK.ttf"));

FontFamily family=new FontFamily("Dark Garden",pfc);
Font dgFont=new Font(family,40);

Bitmap tmpBitmap = new Bitmap(400,100,PixelFormat.Format32bppArgb);
Graphics objGraphics = Graphics.FromImage(tmpBitmap);


objGraphics.DrawString("Hello World!", dgFont, new
SolidBrush(Color.White ),0,0);

Response.Clear();
Response.ContentType = "image/jpeg";
tmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);

Response.End();
tmpBitmap.Dispose();

}
catch(Exception ex)
{
Response.Write("<br>" + ex.Message);
}

}

============================================

In addtion, below is the codeproject article I refer to , that has the more
detailed explanition.

#C# Barcode Generator WebService
http://www.thecodeproject.com/cs/webservices/wsbarcode.asp

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I'm just curious with this code.
tmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);
This line makes the browser display a image only or can you include HTML
Tags as well, as in the following code:

Response.Write("<a href='www.server.com'>Home</a><br><hr>");
tmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);

Response.Write("<hr><a href='www.server.com'>Home</a>");
 
Hi Gabe,

Of course not, one response stream can only be specified one content-type.
IF we return image stream, we must clear other content. That's why I use

Response.Clear();//clear other content
Response.ContentType = "image/jpeg";
tmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);
Response.End();// end the current response and render to client

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top