Windows Develop Bookmark and Share   
 index > Windows Forms General > Drawing on pictureBox
 

Drawing on pictureBox

Hi, I am making a scrolling text with transparent background.

However, thepictureBox1 seem to draw nothing and the string is drawn on Form1.

Any idea?


public partial class Form1 : Form
{
private String drawString = "Sample Text";
private Font drawFont = new Font("Arial", 31);
private SolidBrush drawBrush = new SolidBrush(Color.White);
private int textX = 0;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
timer1.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX++, 0));
}

private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
}


ddlam  Thursday, August 31, 2006 2:08 PM
a picturebox control loads and shows the images from a file, jpeg/bitmap etc...
ahmedilyas  Thursday, August 31, 2006 2:34 PM
a picturebox control loads and shows the images from a file, jpeg/bitmap etc...
ahmedilyas  Thursday, August 31, 2006 2:34 PM
Is there any way I can draw to the upperest?
ddlam  Thursday, August 31, 2006 3:47 PM

sorry don't quite follow- upperest?

Are you not able to draw on the form itself?

ahmedilyas  Thursday, August 31, 2006 3:49 PM

Hi,

you wired your Paint event in a way that the drawing is actually happening on the form, not the picturebox. To draw the text on a picturebox, modify your code like:

public partial class Form1 : Form
{
private String drawString = "Sample Text";
private Font drawFont = new Font("Arial", 31);
private SolidBrush drawBrush = new SolidBrush(Color.White);
private int textX = 0;
public Form1()
{
InitializeComponent();
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
timer1.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX++, 0));
}
private void timer1_Tick(object sender, EventArgs e)
{
this.pictureBox1.Invalidate();
}
}

Andrej

Andrej Tozon  Friday, September 01, 2006 10:45 AM

You can use google to search for other answers

Custom Search

More Threads

• checking connection to the internet
• Cannot programatically change papersource.
• Windows firewall problem
• MsAgent
• Form Graphics
• Gatagridview control - WrapMode property
• MDI Container Event Firing
• Drag and Drop design issues
• a Bug in .NET with HardMarginX, please help!
• Remote system access using WMI