我尝试使用以下代码读取位图文件:
public void open(object o, EventArgs e)
{
fileOpenen.Filter = "Pictures (.BMP/.JPG/.GIF/.PNG)|*.bmp;*.jpg;*.gif;*.png";
if (fileOpenen.ShowDialog() == DialogResult.OK)
{
Bitmap open = new Bitmap(fileOpenen.FileName);
schets.bitmap = open;
this.nieuw(o, e);
}
它应该将位图加载到" schets",它由以下代码组成:
class Schets
{
public Bitmap bitmap; // private naar public
public Schets()
{
bitmap = new Bitmap(1, 1);
}
public Graphics BitmapGraphics
{
get { return Graphics.FromImage(bitmap); }
}
public void VeranderAfmeting(Size sz)
{
if (sz.Width > bitmap.Size.Width || sz.Height > bitmap.Size.Height)
{
Bitmap nieuw = new Bitmap( Math.Max(sz.Width, bitmap.Size.Width)
, Math.Max(sz.Height, bitmap.Size.Height)
);
Graphics gr = Graphics.FromImage(nieuw);
gr.FillRectangle(Brushes.White, 0, 0, sz.Width, sz.Height);
gr.DrawImage(bitmap, 0, 0);
bitmap = nieuw;
}
}
public void Teken(Graphics gr)
{
gr.DrawImage(bitmap, 0, 0);
}
}
我可以从硬盘驱动器中选择.bmp文件,但是当我尝试加载它时,我收到以下错误:
An unhandled exception of type 'System.NullReferenceException' occurred in Schets.exe
答案 0 :(得分:1)
从strack跟踪中你可以看到' System.NullReferenceException' ocucure。比它指向您尝试访问尚未初始化的对象的行。只需初始化对象即可。