Results 1 to 6 of 6

Thread: Drawing strings on a new image, stupid problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Drawing strings on a new image, stupid problem

    Hi,

    This may be a really stupid question but I dunno how to solve my issue

    My goal is actually very simple: I simply want a (shared) function that accepts a List(Of String) which creates an image and writes those strings (line by line) onto the image. There's a bit more to it than that, but for the sake of example this is all you need to know.

    Usually when drawing on an image I would create a Bitmap object and get the graphics object from Graphics.FromImage:
    Code:
    Dim bmp As New Bitmap(100, 100)
    Using g As Graphics = Graphics.FromImage(bmp)
       g.FillRectangle(...)
       g.DrawString(..)
    End Using
    bmp.Save(...)
    Seems easy enough, but the problem is that I don't know how many strings there will be in the list, nor how long they will be. The image should 'scale itself' according to the strings in the list. If the list contains 5 strings it should be 5 strings high (plus a little extra for borders). If the list contains only 1 string it should be only one string high.
    Same goes for the width, if the longest string is 150 pixels, the image needs to be at least 150 pixels (160, 170 or something).

    This is all good, I can get the dimensions of the strings by using Graphics.MeasureString. The problem now is that I need the dimensions of the image to create the bitmap. I need the Graphics object to get the dimensions. But, I need the bitmap to get the Graphics object I'm stuck in a loop here, if I don't have the Graphics object (I can't create it from nothing..) then I cannot measure the strings, but if I cannot measure the strings then I cannot determine how large my image will be, so I cannot create a bitmap and associated Graphics object...?!

    I looked through the obvious methods and properties like Width/Height of the Bitmap but of course they are read-only so that's no use...

    There must be a simple solution to this? What am I missing?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing strings on a new image, stupid problem

    Use TextRenderer.MeasureText instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Apr 2008
    Posts
    193

    Re: Drawing strings on a new image, stupid problem

    Could you use Me.CreateGraphics and measure your strings there (without drawing them).
    Then Create the bitmap based on the measurements you took.
    Then draw in the Graphics.FromImage(your new image).
    Seems convlouted but workable (I'm clearly no expert!)
    Good luck

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing strings on a new image, stupid problem

    Quote Originally Posted by CodeDabbler View Post
    Could you use Me.CreateGraphics and measure your strings there (without drawing them).
    Then Create the bitmap based on the measurements you took.
    Then draw in the Graphics.FromImage(your new image).
    Seems convlouted but workable (I'm clearly no expert!)
    Good luck
    One thing to note: there's no specific indication that this code is in a class that inherits Control, so Me.CreateGraphics may not even exist.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Drawing strings on a new image, stupid problem

    Quote Originally Posted by jmcilhinney View Post
    One thing to note: there's no specific indication that this code is in a class that inherits Control, so Me.CreateGraphics may not even exist.
    Correct. I even mentioned that it was a shared function so even if the class did inherit Control I still couldn't use it. Well I suppose I could have not made it shared but then I wouldn't have mentioned it.

    From the MSDN page I think TextRenderer.MeasureText seems to do what I want. I did recall that there might have been another method to measure text but I could never remember it. I must make a mental note not to forget it again

    Thanks!

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Drawing strings on a new image, stupid problem

    The Graphics.MeasureString and the textRenderer measurement often come out slightly different; you may want to use TextRenderer to actually draw the text, also.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width