C#为什么我的代码在picturebox上画的图存储不了?
以下为我的代码publicpartialclassForm1:Form{privatePointp1=newPoint(0,0);privatePointp2=newPo...
以下为我的代码
public partial class Form1 : Form
{
private Point p1 = new Point(0, 0);
private Point p2 = new Point(0, 0);
private bool m_bMouseDown = false;
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofdlg = new OpenFileDialog();
ofdlg.Filter = "BMP File(*.*)|*.*";
if (ofdlg.ShowDialog() == DialogResult.OK)
{
Bitmap image = new Bitmap(ofdlg.FileName);
pictureBox1.Image = image;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (!m_bMouseDown)
{
p1 = new Point(e.X, e.Y);
p2 = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label2.Text = e.X.ToString();
label3.Text = e.Y.ToString();
Graphics g = pictureBox1.CreateGraphics();
if (e.Button == MouseButtons.Left)
{
g.DrawLine(new Pen(Color.Red, 2), p1, p2);
p2 = e.Location;
g.DrawLine(new Pen(Color.Blue, 2), p1, p2);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap box1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
box1.Save(@"E:\temp.jpg");
MessageBox.Show(@"已经保存为E:\temp.jpg!");
} 展开
public partial class Form1 : Form
{
private Point p1 = new Point(0, 0);
private Point p2 = new Point(0, 0);
private bool m_bMouseDown = false;
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofdlg = new OpenFileDialog();
ofdlg.Filter = "BMP File(*.*)|*.*";
if (ofdlg.ShowDialog() == DialogResult.OK)
{
Bitmap image = new Bitmap(ofdlg.FileName);
pictureBox1.Image = image;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (!m_bMouseDown)
{
p1 = new Point(e.X, e.Y);
p2 = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label2.Text = e.X.ToString();
label3.Text = e.Y.ToString();
Graphics g = pictureBox1.CreateGraphics();
if (e.Button == MouseButtons.Left)
{
g.DrawLine(new Pen(Color.Red, 2), p1, p2);
p2 = e.Location;
g.DrawLine(new Pen(Color.Blue, 2), p1, p2);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap box1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
box1.Save(@"E:\temp.jpg");
MessageBox.Show(@"已经保存为E:\temp.jpg!");
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询