Drawing strings on a new image, stupid problem
Hi,
This may be a really stupid question but I dunno how to solve my issue :confused:
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 :confused: 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?
Re: Drawing strings on a new image, stupid problem
Use TextRenderer.MeasureText instead.
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
Re: Drawing strings on a new image, stupid problem
Quote:
Originally Posted by
CodeDabbler
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.
Re: Drawing strings on a new image, stupid problem
Quote:
Originally Posted by
jmcilhinney
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:lol:
Thanks!
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.