Windows Develop Bookmark and Share   
 index > Windows Forms General > label with transparent background
 

label with transparent background

Hi, I am making a scrolling text over a picture box. However,I can't hide the label background to show out the picture box. Any idea?
ddlam  Saturday, March 11, 2006 5:34 AM

You have to use the Graphics object to draw string in a picture box. You need to adjust the co-ordinates to suit your needs in following code:

private void pictureBox1_Paint(object sender, PaintEventArgs e)

{

DrawStringRectangleF(e);

}

//

//Sample code taken from MSDN help on Graphics.DrawString method

//

public void DrawStringRectangleF(PaintEventArgs e)

{

// Create string to draw.

String drawString = "Sample Text";

// Create font and brush.

Font drawFont = new Font("Arial", 16);

SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create rectangle for drawing.

float x = 150.0F;

float y = 150.0F;

float width = 200.0F;

float height = 50.0F;

RectangleF drawRect = new RectangleF( x, y, width, height);

// Draw rectangle to screen.

Pen blackPen = new Pen(Color.Black);

e.Graphics.DrawRectangle(blackPen, x, y, width, height);

// Draw string to screen.

e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);

}

Yogesh Prabhu  Friday, March 17, 2006 5:27 PM
Did you try setting the Label's BackColor property to Transparent? Also, trydrawing your textdirectly onto the PictureBox in its Paint event.
CommonGenius.com  Saturday, March 11, 2006 10:09 PM

I type the codes like this but It showes nothing

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
PictureBox1.Text = "DRAW TEXT HERE"
End Sub

ddlam  Friday, March 17, 2006 4:58 PM
The text property of the PictureBox is never displayed. You have to draw onto the graphics context using the Graphics object in the PaintEventArgs.
CommonGenius.com  Friday, March 17, 2006 5:19 PM

You have to use the Graphics object to draw string in a picture box. You need to adjust the co-ordinates to suit your needs in following code:

private void pictureBox1_Paint(object sender, PaintEventArgs e)

{

DrawStringRectangleF(e);

}

//

//Sample code taken from MSDN help on Graphics.DrawString method

//

public void DrawStringRectangleF(PaintEventArgs e)

{

// Create string to draw.

String drawString = "Sample Text";

// Create font and brush.

Font drawFont = new Font("Arial", 16);

SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create rectangle for drawing.

float x = 150.0F;

float y = 150.0F;

float width = 200.0F;

float height = 50.0F;

RectangleF drawRect = new RectangleF( x, y, width, height);

// Draw rectangle to screen.

Pen blackPen = new Pen(Color.Black);

e.Graphics.DrawRectangle(blackPen, x, y, width, height);

// Draw string to screen.

e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);

}

Yogesh Prabhu  Friday, March 17, 2006 5:27 PM
It works extremely nice. Thanks you guys so much!
ddlam  Friday, March 17, 2006 6:35 PM
Đonny  Sunday, May 13, 2007 6:06 PM
This is another way to achieve it :

Code Block

Label1.BackColor= Color.Transparent

Label1.Parent=PictureBox1


marcexx662  Wednesday, December 26, 2007 8:06 PM
There are two different meaning of "transparency".
  1. Control is transparent in relation to background of it's container. This means that you can place i.e. Label in i.e. Panel (or PictureBox - but this cannot be done in design-time) and set Label's BackColor to Transparent (or some color with alpha-channel). This does not actualy make Label's background transparent. This instructs label to observe background of its container and create Label's background from container's background in place where label is placed. You cannot make label to allow another label (or other control) to be seen through itself. It is because Label has its own completely solid background, but calculated from background of its container in order to look like transparent.
  2. Control is really transparent (or semi-transparent). This mean that it does not paint its background (or it paints it with semi-transparent color). In this case you can place one control over another and controls can bee seen one througn another.
  3. Another way how to make control to behave "transaprent" is to shape it to some strang shape - so, its regoin is only region i.e. of Label's Text.
  1. This is how most of .NET Windows Forms controls behaves.
  2. This can be done via deriving from Windows Forms controls and overriding its OnPainBackground. But before that you must instruct control to be transparent - to have custom-painted background - and this is OS-dependent. IMHO it can be successfully done on XP and Vista (and may be 2000) but not 95/98/ME or NT.
  3. It is very time-consuming (needs computing power) and very tricky for Label. And can be successfully implemented IMHO only for static controls (Label, Button, PictureBox; not for i.e. TextBox).
I, now in 2008 :-), suggest using WPF.
Đonny  Tuesday, January 01, 2008 8:31 PM
This works for me, thanks a lot pal...
:)
Soma sundaram P  Monday, September 14, 2009 11:38 AM

You can use google to search for other answers

Custom Search

More Threads

• Winforms - Which event to use to ensure program cleanup is done when program ends?
• Help with Custom User Control
• Maskedtextbox Bug?
• Thread Safe Form Controls and components
• Messagebox does not show when using barcode wedge scanner
• TextFormatFlags.TabStop??
• Twain_32.dll and Twain ver. 1.8 specification compatibility.
• C# Grid Drawing Technique help.
• Graph Library Controls Copyright Question
• Sending value back into a do while from another form.