Is this code the best way to measure a string?

A

Academia

I need to create a Bitmap just big enough to display a string.

Since I need a Graphic object to measure the string I create and dispose a
Bitmap and a Graphic object only for that purpose.

Isn't there a better way of doing this than with the code below?

Thanks

Dim bit As Bitmap = New Bitmap(SomeWidth, SomeHeight)

Dim gr As Graphics = Picture.CreateGraphics(Nothing, bit)

Dim f As Font = New Font("Arial", 10)

Dim b As Brush = New SolidBrush(Color.Black)

Dim s As Size = gr.MeasureString(strng, f).ToSize

bit.dispose()

bit = New Bitmap(s.Width, s.Height)

gr.dispose()

gr = Picture.CreateGraphics(Nothing, bit)

Gr.DrawString(Strng, f, b, 0, 0)
 
C

Cor Ligthert[MVP]

Academia,

I have never seen a better one that that one here,

Typical a Wagner solution

Cor
 
R

rowe_newsgroups

Don't know what the last sentence means, but thanks for the help.








- Show quoted text -

He's talking about Herfried Wagner another frequent poster here.

Thanks,

Seth Rowe
 
H

Herfried K. Wagner [MVP]

Academia said:
I need to create a Bitmap just big enough to display a string.

Since I need a Graphic object to measure the string I create and dispose a
Bitmap and a Graphic object only for that purpose.

Your approach is okay, but I'd use 'Using' blocks in order to encapsulate
the calls to 'Dispose' (see documentation on the 'Using' statement).
 
A

Academia

rowe_newsgroups said:
He's talking about Herfried Wagner another frequent poster here.

Thanks,

Seth Rowe

And frequently my helper.
Just didn't get the connection.
Especially now that Wagner has suggested a modification.


Thanks :)
 
A

Academia

I'll check that.

Thanks

Herfried K. Wagner said:
Your approach is okay, but I'd use 'Using' blocks in order to encapsulate
the calls to 'Dispose' (see documentation on the 'Using' statement).
 

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

Top