Windows窗体应用程序中pictureBox的问题
代码很简单,picLoad是加载一张图,Draw是画个框。
如下,按钮1是加载,按钮2是画框,按钮3是先加载再画框。
先按button1 ,再按button2,正常运行。
如果只按Button3,却不会画出框来,只加载了图片。
请问怎么办???我不想分步啊。。。
(经过研究,按BUTTON3的时候,系统好像在加载图片之前先画了框了。。。这可如何是好)
(picLoad里现在用的是picBox.Image=xxx的方法。一样没用。。。和注释掉的load方法一样的情况)
- C# code
void PicLoad() { Image i = Image.FromFile(@"D:\temp.bmp"); this.pictureBox1.Image = i; //this.pictureBox1.Load(@"D:\temp.bmp"); } void Draw() { Graphics g = this.pictureBox1.CreateGraphics(); g.DrawRectangle(new Pen(Color.Gold), 0, 0, 100, 100); } private void button1_Click(object sender, EventArgs e) { PicLoad(); } private void button2_Click(object sender, EventArgs e) { Draw(); } private void button3_Click(object sender, EventArgs e) { PicLoad(); Draw(); }
[解决办法]
调用Image时,或导致窗体刷新,是不是在刷新过程中执行了画框的方法;
改成在Image上画框好了:
void Draw()
{
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.DrawRectangle(new Pen(Color.Gold), 0, 0, 100, 100);
}
[解决办法]
放PictrueBox的OnPaint事件中试试看。
[解决办法]
最好在OnPaint中绘画,否则窗口一刷新,像最小化,再最大化,画的东西就没有了。