Render Image from Font

Discussion in 'ASP .Net' started by localhost, Oct 11, 2004.

  1. localhost

    localhost Guest

    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.
     
    localhost, Oct 11, 2004
    #1
    1. Advertising

  2. localhost

    Patrice Guest

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

    Patrice

    --

    "localhost" <> a écrit dans le message de
    news:...
    >
    > 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.
    >
     
    Patrice, Oct 11, 2004
    #2
    1. Advertising

  3. localhost

    Gabe Garza Guest

    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


    "localhost" <> wrote in message
    news:...
    >
    > 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.
    >
     
    Gabe Garza, Oct 11, 2004
    #3
  4. 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.)
     
    Steven Cheng[MSFT], Oct 12, 2004
    #4
  5. localhost

    Gabe Garza Guest

    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>");


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.)
    >
    >
     
    Gabe Garza, Oct 12, 2004
    #5
  6. 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.)
     
    Steven Cheng[MSFT], Oct 13, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Hungus

    Font class, external font files question

    Karl Hungus, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    549
    Karl Hungus
    Feb 4, 2004
  2. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,093
    Lau Lei Cheong
    May 15, 2004
  3. mttc
    Replies:
    2
    Views:
    2,425
    Roedy Green
    Jul 3, 2009
  4. Laszlo Nagy
    Replies:
    1
    Views:
    652
    David Boddie
    Sep 19, 2009
  5. Glenn Gillen
    Replies:
    0
    Views:
    288
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page