"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:vq******** ***@twister.nyr oc.rr.com:
Sorry I wasn't clear enough, the rectangle is around a number of text
lines. The number of text lines can be any where between 8 and 45
lines. I do not know how many lines up front. I am trying to avoid
going through and calculating the lines, then create the rectangle and
fill, then go back and actually create the text lines.
I was just wondering if I could fill a rectangle around all the text
with color without covering up the text.
"hoppy" <no***@invalid. null> wrote in message
news:Xn******** **********@194. 117.143.53... "DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:jt******** *********@twist er.nyroc.rr.com :
I have a rectangle around text that I want to fill in with color. I
do not know the height of the rectangle until I actually go through
and draw out the text. Is there a way of filling in the rectangle
that will allow the text to show through?
I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)
but that isn't exactly the effect I wanted.
Is the only way to do this is to actually calculate what the height
of the rectangle first, fill in the color then draw the text lines?
Use measurestring to get the wodth and height without actually
drawing the string.
Dim sourceText As String = sender.Text
Dim measureStringBi tmap As New Bitmap(CInt(sen der.ClientSize. Width),
_
CInt(sender.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = Graphics.FromIm age(measureStri ngBitmap)
' need to set the string format to get line-wraps
Dim textStringForma t As StringFormat = _
CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString
Graphics.Measur eString(sourceT ext, _
sender.Font, New SizeF(sender.Cl ientSize.Width, _
sender.ClientSi ze.Height), textStringForma t, Nothing, Nothing)
measureStringGr aphics.Dispose( )
'textSize now contins the width and height that the text takes up
when 'draw with the given font and stringformat.
Not sure what you mean..
This will make a bitmap from a string, set the backcolor to orange, and
wrap the text to the number of lines required to fit the text. (so long
as label1 is bit enough to hold all the text).
With a form with these controls:
textbox1 for entering the text
label1 the size of this is the size of the bit of papaer, or control or
form or whatever it is that you want the text to be scaled for (so it's
width is the right size),
button1
and a picturebox to show the resultant bitmap:
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
'fill label1 with text from textbox1
Dim sourceText As String = TextBox1.Text
'it will draw into label1, we pass the width and height.
'The height isn't really important here. If the text
'doesn't fit then it gets clipped. If the text fits
'easily then the size returned is just the size of the text.
Dim measureStringBi tmap As New _
Bitmap(CInt(Lab el1.ClientSize. Width), _
CInt(Label1.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = _
Graphics.FromIm age(measureStri ngBitmap)
'setting stringformat will ensure the lines wrap.
Dim textStringForma t As StringFormat _
= CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
'we can get the number of text lines that are drawn
'and the number of characters that fit into the final size by
'passing a couple of variables byref
Dim charsFitted As Integer
Dim linesUsed As Integer
Dim textSize As New SizeF
'draw the string from textbox1
'into an area the size of label1,
'using the font from textbox1
'using a stringformat to wrap the lines
'get the size of rectangle required to fit
'this text.
textSize = _
measureStringGr aphics.MeasureS tring(sourceTex t, TextBox1.Font, _
New SizeF(Label1.Cl ientSize.Width, Label1.ClientSi ze.Height), _
textStringForma t, charsFitted, linesUsed)
Debug.WriteLine ("Chars fitted into label: " & charsFitted & _
" of " & sourceText.Leng th)
Debug.WriteLine ("Lines fitted into label: " & linesUsed)
measureStringGr aphics.Dispose( )
measureStringBi tmap.Dispose()
'create a bitmap of the text, with backcolor set
Dim stringBitmap As New Bitmap(CInt(tex tSize.Width), _
CInt(textSize.H eight))
'create a graphics to draw on
Dim g As Graphics = Graphics.FromIm age(stringBitma p)
'fill the back of the bitmap with a color
g.Clear(System. Drawing.Color.O range)
'draw the string
Dim b As New SolidBrush(Colo r.Black)
Dim recF As New RectangleF(0.0F , 0.0F, _
textSize.Width, textSize.Height )
g.DrawString(so urceText, TextBox1.Font, _
b, recF, textStringForma t)
b.Dispose()
textStringForma t.Dispose()
g.Dispose()
'now you can draw the bitmap where you want the string.
'I'll just show it in a picturebox..
Me.PictureBox1. Image = stringBitmap
End Sub