一道关于C#的问题!!!C# Form编程的“未处理NullReferenceException”错误。

来源:百度知道 编辑:UC知道 时间:2024/06/17 12:30:49
private void hecheng_Click(object sender, EventArgs e)
{
Bitmap box2 = new Bitmap(pictureBox2.Image);
Bitmap box1 = new Bitmap(pictureBox1.Image);
Bitmap box3 = new Bitmap(pictureBox3.Image);
Color c1 = new Color();
Color c2 = new Color();
int r, g, b;
int pw, ph;
pw = pictureBox1.Width;
ph = pictureBox1.Height;
if (pictureBox1.Image.Width < pictureBox1.Width)
{
pw = pictureBox1.Image.Width;

}
if (pictureBox1.Image.Height < pictureBox1.Height)
{
ph = pictureBox1.Image.Height;
}
if (pictureBox2.Width > pictureBox2.Image.Width)
{
pw = pictureBox2.Image.Width;
}
if (pictureBox2.Heig

pictureBox2.Image,pictureBox1.Image和pictureBox3.Image为null(即没有图片)所以出错,你只要加上图片就不会出错了或者创建位图时Bitmap box1 = new Bitmap(200,100);这样也可以!

Bitmap box2 = new Bitmap(文件的物理地址,或者文件的长,宽);

拖上去的PictureBox里面的Image默认是null的。初始化一下。