如何从按钮单击事件调用绘制事件? [英] How to call paint event from a button click event?
问题描述
我有一个带有事件paint"的图片框,我还在那里获得了用于绘制图形的代码.
I have a pictureBox with the event "paint" and I also got code in there to draw graphics.
例如:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
graphics.DrawEllipse(Pens.Blue, 10, 10, 100, 100);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
}
}
我的问题是.如何从 button_click 事件触发这些事件?我在网上搜索了很多,发现了很多 awnsers,如.invalidate()"或.Refresh()"和.Update".但是我的作业告诉我需要使用 .Refresh() 方法来完成,并且绘画需要在一个 pictureBox 中.
My question is. How to fire these events of from a button_click event? I searched alot on the web and found alot of awnsers like ".invalidate()" or ".Refresh()" and ".Update". But my assignment tells me that I need to do it with the .Refresh() method and the painting needs to be in a pictureBox.
我从 .Refresh() 方法中注意到的.是它擦除了图片框(绘制图片框是如何在初始化时创建的).所以在按钮中触发 .Refresh 方法对我不起作用.
What I notice from .Refresh() method. Is that it erases the pictureBox(draws the pictureBox how it was created on initialize). So firing off .Refresh method in a button did not work for me.
关于如何从按钮触发绘画事件的任何其他建议?
Any other suggestions how to fire off a paint event from a button?
推荐答案
这应该可以解决问题:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void button1_Click(object sender, EventArgs e)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawEllipse(Pens.Blue, 10, 10, 100, 100);
pictureBox1.Refresh();
}
}
}
这篇关于如何从按钮单击事件调用绘制事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!