GDI+ and placing text inside a bitmap

Discussion in 'ASP .Net' started by Jeff, Nov 6, 2008.

  1. Jeff

    Jeff Guest

    hey

    asp.net 2.0

    Below you see my code.

    #1
    How to place the text at the buttom right of the image? I mean how do I
    calculate the x,y parameter of the DrawString method?

    #2
    How can I place the text vertically centered in the image. Also here I mean
    how do calculate the x,y parameters of DrawString

    MemoryStream ms = new MemoryStream(test.FileBytes);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
    Bitmap img = new Bitmap(new Bitmap(image));

    Debug.WriteLine("Height = " + img.Height.ToString());
    Font font = new Font("Verdana", 12);
    Graphics gfx = Graphics.FromImage(img);
    gfx.DrawString("Hello World", font, Brushes.Black, 5, img.Height - 12);
    img.Save(Response.OutputStream, ImageFormat.Gif);

    (as you see in my code, I'm using font size 12. Then I thought it was a good
    idea to subtract 12 from the height. But that didn't solve the problem -
    half of the text was drawn below the image)

    any suggestions?
     
    Jeff, Nov 6, 2008
    #1
    1. Advertising

  2. Jeff

    Jeff Guest

    I think I've solved it using StringFormat.Aligment.
     
    Jeff, Nov 6, 2008
    #2
    1. Advertising

  3. Jeff

    bruce barker Guest

    normally you'd call MeasureString to get the bonding rect, then calc where to
    draw.

    -- bruce (sqlwork.com)


    "Jeff" wrote:

    > hey
    >
    > asp.net 2.0
    >
    > Below you see my code.
    >
    > #1
    > How to place the text at the buttom right of the image? I mean how do I
    > calculate the x,y parameter of the DrawString method?
    >
    > #2
    > How can I place the text vertically centered in the image. Also here I mean
    > how do calculate the x,y parameters of DrawString
    >
    > MemoryStream ms = new MemoryStream(test.FileBytes);
    > System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
    > Bitmap img = new Bitmap(new Bitmap(image));
    >
    > Debug.WriteLine("Height = " + img.Height.ToString());
    > Font font = new Font("Verdana", 12);
    > Graphics gfx = Graphics.FromImage(img);
    > gfx.DrawString("Hello World", font, Brushes.Black, 5, img.Height - 12);
    > img.Save(Response.OutputStream, ImageFormat.Gif);
    >
    > (as you see in my code, I'm using font size 12. Then I thought it was a good
    > idea to subtract 12 from the height. But that didn't solve the problem -
    > half of the text was drawn below the image)
    >
    > any suggestions?
    >
    >
    >
     
    bruce barker, Nov 6, 2008
    #3
    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. =?Utf-8?B?cGJ1bQ==?=

    GDI+ - limits for pixel dimensions in a bitmap object

    =?Utf-8?B?cGJ1bQ==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    369
    =?Utf-8?B?cGJ1bQ==?=
    Jun 18, 2004
  2. Replies:
    7
    Views:
    18,995
    takenaka
    Oct 12, 2007
  3. Gandalf

    Masked bitmap from bitmap

    Gandalf, Jan 29, 2004, in forum: Python
    Replies:
    0
    Views:
    612
    Gandalf
    Jan 29, 2004
  4. Robin van de Water [synthesiSFactory]

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water [synthesiSFactory], Jan 10, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    1,383
    Robin van de Water [synthesiSFactory]
    Jan 10, 2007
  5. xqggz
    Replies:
    1
    Views:
    449
    Victor Bazarov
    Jun 19, 2007
Loading...

Share This Page