C# 用drawline在bitmap中绘图并显示在picturebox中
有两个picturebox控件,分别用drawline在bitmap中绘图,再把两个bitmap显示到picturebox中,打开form的时候第一个picturebox...
有两个picturebox控件,分别用drawline在bitmap中绘图,再把两个bitmap显示到picturebox中,打开form的时候第一个picturebox中没有图形,第二个显示正常,拖动form,两个picturebox中的内容显示正常
代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
EBitmap bit1 = new EBitmap();
EBitmap bit2 = new EBitmap();
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
bit1.DrawLine();
pictureBox1.Image = (Image)bit1.bp;
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
bit2.DrawLine();
pictureBox2.Image = (Image)bit2.bp;
}
class EBitmap
{
public Bitmap bp = new Bitmap(100, 100);
Pen p = new Pen(Color.Black, 1);
public void DrawLine()
{
Graphics g = Graphics.FromImage(bp);
g.DrawLine(p, 1, 1, 100, 100);
}
}
} 展开
代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
EBitmap bit1 = new EBitmap();
EBitmap bit2 = new EBitmap();
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
bit1.DrawLine();
pictureBox1.Image = (Image)bit1.bp;
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
bit2.DrawLine();
pictureBox2.Image = (Image)bit2.bp;
}
class EBitmap
{
public Bitmap bp = new Bitmap(100, 100);
Pen p = new Pen(Color.Black, 1);
public void DrawLine()
{
Graphics g = Graphics.FromImage(bp);
g.DrawLine(p, 1, 1, 100, 100);
}
}
} 展开
展开全部
添加 Form_Load 事件,在事件里面加上 pictureBox1.Invalidate();和pictureBox2.Invalidate();即可解决。
你再Paint事件里面 设置pictureBox1.Image 容易死循环
最好的解决办法是:
bit1.DrawLine();
pictureBox1.Image = (Image)bit1.bp;
bit2.DrawLine();
pictureBox2.Image = (Image)bit2.bp;
放到Form_Load事件里面就可以了,不需要Paint事件
你再Paint事件里面 设置pictureBox1.Image 容易死循环
最好的解决办法是:
bit1.DrawLine();
pictureBox1.Image = (Image)bit1.bp;
bit2.DrawLine();
pictureBox2.Image = (Image)bit2.bp;
放到Form_Load事件里面就可以了,不需要Paint事件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询