我的程序允许用户在类似于 MS 绘画的方式中绘制图片框,现在我正在尝试将图片框保存为 .jpg 文件,但尝试执行时出现空异常错误所以。
编辑:应该提到这是一个 NullReferenceException
这是我收到异常错误的保存按钮:
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
}
这是我的其余代码:
public Form2()
{
InitializeComponent();
//creates items for combobox brush sizes
for (int i = 1; i <= 20; i++)
{
string[] numbers = { i.ToString() };
comboBox1.Items.AddRange(numbers);
}
}
bool paint = false;
SolidBrush color = new SolidBrush(Color.Black);
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint == true)
{
int brushSize = Convert.ToInt32(comboBox1.SelectedItem);
Graphics g = pictureBox1.CreateGraphics();
if (comboBox1.SelectedIndex > 0)
{
g.FillEllipse(color, e.X, e.Y, brushSize, brushSize);
}
else
{
g.FillEllipse(color, e.X, e.Y, 10, 10);
}
g.Dispose();
}
}
//button that opens colour dialog box
private void button1_Click_1(object sender, EventArgs e)
{
ColorDialog cld = new ColorDialog();
if (cld.ShowDialog() == DialogResult.OK)
{
color = new SolidBrush(cld.Color);
}
}
//Button that clears pictureBox
private void Button2_Click_1(object sender, EventArgs e)
{
Graphics g1 = pictureBox1.CreateGraphics();
g1.Clear(pictureBox1.BackColor);
}