Background color of rectangle

  • Thread starter Thread starter DazedAndConfused
  • Start date Start date
D

DazedAndConfused

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?
 
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.
 
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.
 
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
 
I am using the PrintDocument Control (and PrintPreview) and using
Graphics.DrawString to put out the text. The text uses 2 different fonts
depending on what text is generated.

I store the starting position of Y (I already know where x begins and ends).
When all the text is generated I get the finnal position of Y and draw a
rectangle around the text. (actualy I draw two rectangles and a few vertical
line to give kind of a grid effect, but completely fill the outer most
rectangle)

what I wanted to do is fill the rectangle with color before I did:
e.Graphics.DrawRectangle(reportPen, 10, gridStartY, x, gridEndY -
gridStartY)

but if I do:

e.Graphics.FillRectangle(fillBrush, 10, gridStartY, x, gridEndY -
gridStartY)

It covers up the text already written and I was wondering if there was a way
to use e.Graphics.FillRectangle that would allow the text to show as if it
was ontop of the FillRectangle.

I was able to change my logic so that I could get the start position and
calculate the height of the rectangle and fill it before I start generating
the text using Graphics.DrawString.

I was just wondering if there was a way to use
e.Graphics.FillRectangle(fillBrush, 10, gridStartY, e.PageBounds.Width - 15,
gridEndY - gridStartY) that would not cover up the text on the report if it
was generated first.
 
you have to fill first, then draw your text.

i've done the same type of thing before that you're doing. first i fill the
client area, next draw any borders needed, then i drawstring. fillrectangle
is doing exactly what it should...it's just that in this order, it fills in
over the text you've already output...just change your order of operations.

hth,

me
 

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

Back
Top