C#中出现“未将对象引用设置到对象的实例”错误
publicpartialclassForm1:Form{Graphicsg;Imageimage;privatevoidbutton1_Click(objectsend...
public partial class Form1 : Form
{
Graphics g;
Image image;
private void button1_Click(object sender, EventArgs e)
{
Graphics g;
Image image;
image = Image.FromFile("01.jpg");
g = this.CreateGraphics();
g.DrawImage(image, 100, 100);
}
private void button3_Click(object sender, EventArgs e)
{
Color curColor;
Bitmap bitmap = new Bitmap(image);
int ret;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
curColor = bitmap.GetPixel(i, j);
ret = (int)(curColor.R * 0.587 + curColor.B * 0.114);
bitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
}
g = this.CreateGraphics();
g.DrawImage(bitmap, 100, 100);
}
}
private void button6_Click(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
image.Save("abc.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
else if (radioButton1.Checked)
{
image.Save("abc.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (radioButton3.Checked)
{
image.Save("abc.gif", System.Drawing.Imaging.ImageFormat.Gif);
}
else if (radioButton4.Checked)
{
image.Save("abc.icon", System.Drawing.Imaging.ImageFormat.Icon);
}
else if (radioButton5.Checked)
{
image.Save("abc.tiff", System.Drawing.Imaging.ImageFormat.Tiff);
}
}
private void button5_Click(object sender, EventArgs e)
{
g.RotateTransform(60);
g.DrawImage(image,1,1);
}
private void button2_Click(object sender, EventArgs e)
{
g.DrawImage(image,new Rectangle(200,200,400,400),new Rectangle(0,0,image.Width,image.Height),GraphicsUnit.Pixel);
}
private void pictureBox1_Click_1(object sender, EventArgs e)
{
}
} 展开
{
Graphics g;
Image image;
private void button1_Click(object sender, EventArgs e)
{
Graphics g;
Image image;
image = Image.FromFile("01.jpg");
g = this.CreateGraphics();
g.DrawImage(image, 100, 100);
}
private void button3_Click(object sender, EventArgs e)
{
Color curColor;
Bitmap bitmap = new Bitmap(image);
int ret;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
curColor = bitmap.GetPixel(i, j);
ret = (int)(curColor.R * 0.587 + curColor.B * 0.114);
bitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
}
g = this.CreateGraphics();
g.DrawImage(bitmap, 100, 100);
}
}
private void button6_Click(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
image.Save("abc.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
else if (radioButton1.Checked)
{
image.Save("abc.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (radioButton3.Checked)
{
image.Save("abc.gif", System.Drawing.Imaging.ImageFormat.Gif);
}
else if (radioButton4.Checked)
{
image.Save("abc.icon", System.Drawing.Imaging.ImageFormat.Icon);
}
else if (radioButton5.Checked)
{
image.Save("abc.tiff", System.Drawing.Imaging.ImageFormat.Tiff);
}
}
private void button5_Click(object sender, EventArgs e)
{
g.RotateTransform(60);
g.DrawImage(image,1,1);
}
private void button2_Click(object sender, EventArgs e)
{
g.DrawImage(image,new Rectangle(200,200,400,400),new Rectangle(0,0,image.Width,image.Height),GraphicsUnit.Pixel);
}
private void pictureBox1_Click_1(object sender, EventArgs e)
{
}
} 展开
展开全部
private void button3_Click(object sender, EventArgs e)
{
Color curColor;
Bitmap bitmap = new Bitmap(image);
int ret;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
curColor = bitmap.GetPixel(i, j);
ret = (int)(curColor.R * 0.587 + curColor.B * 0.114);
bitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
}
g = this.CreateGraphics();
g.DrawImage(bitmap, 100, 100);
}
}
中 Bitmap bitmap = new Bitmap(image); image没有创建对象,即还是为null的状态,所有报空指针异常。
看你的程序里边 是先在 button1_Click 里边初始化 image对象,所以你要么 在运行时,先 点击 button1按钮,再 点击其他的;要么在其他函数运行前,先调用下 button1_Click
更多追问追答
追问
我就是先点击的BUTTON1 但是还是 出现这样的错误
调用BUTTON1_CLICK的函数怎么写。。。。
小白不懂,,
追答
刚刚看错了,你 BUTTON1_CLICK 函数里边的 Image对象创建了一个 本地局部变量,这里是不是错了?应该用那个全局变量?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询