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.
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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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 measureStringBitmap As New _
Bitmap(CInt(Label1.ClientSize.Width), _
CInt(Label1.ClientSize.Height))
Dim measureStringGraphics As Graphics
measureStringGraphics = _
Graphics.FromImage(measureStringBitmap)
'setting stringformat will ensure the lines wrap.
Dim textStringFormat As StringFormat _
= CType(StringFormat.GenericDefault.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 = _
measureStringGraphics.MeasureString(sourceText, TextBox1.Font, _
New SizeF(Label1.ClientSize.Width, Label1.ClientSize.Height), _
textStringFormat, charsFitted, linesUsed)
Debug.WriteLine("Chars fitted into label: " & charsFitted & _
" of " & sourceText.Length)
Debug.WriteLine("Lines fitted into label: " & linesUsed)
measureStringGraphics.Dispose()
measureStringBitmap.Dispose()
'create a bitmap of the text, with backcolor set
Dim stringBitmap As New Bitmap(CInt(textSize.Width), _
CInt(textSize.Height))
'create a graphics to draw on
Dim g As Graphics = Graphics.FromImage(stringBitmap)
'fill the back of the bitmap with a color
g.Clear(System.Drawing.Color.Orange)
'draw the string
Dim b As New SolidBrush(Color.Black)
Dim recF As New RectangleF(0.0F, 0.0F, _
textSize.Width, textSize.Height)
g.DrawString(sourceText, TextBox1.Font, _
b, recF, textStringFormat)
b.Dispose()
textStringFormat.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