Re: Background color of rectangle



"DazedAndConfused" <AceMagoo61@xxxxxxxxx> wrote in
news:vq2Ke.4$%91.1@xxxxxxxxxxxxxxxxxxxx:

> 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" <noone@xxxxxxxxxxxx> wrote in message
> news:Xns96AD8F1F01769blah@xxxxxxxxxxxxxxxxx
>> "DazedAndConfused" <AceMagoo61@xxxxxxxxx> wrote in
>> news:jt1Ke.6329$N93.1737@xxxxxxxxxxxxxxxxxxxx:
>>
>>> 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.GreenYellow)
>>> Dim fillBrush As New SolidBrush(myColor)
>>>
>>> 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 measureStringBitmap As New Bitmap(CInt(sender.ClientSize.Width),
>> _
>> CInt(sender.ClientSize.Height))
>> Dim measureStringGraphics As Graphics
>> measureStringGraphics = Graphics.FromImage(measureStringBitmap)
>>
>> ' need to set the string format to get line-wraps
>> Dim textStringFormat As StringFormat = _
>> CType(StringFormat.GenericDefault.Clone(), StringFormat)
>> Dim textSize As New SizeFtextSize = measureString
>>
>> Graphics.MeasureString(sourceText, _
>> sender.Font, New SizeF(sender.ClientSize.Width, _
>> sender.ClientSize.Height), textStringFormat, Nothing, Nothing)
>>
>> measureStringGraphics.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(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

.


Loading