保存文件对话框未处理的异常
Save file dialog An unhandled exception
我有一些关于 savefiledialog c# 的问题,我在调试时遇到类型为 System.NullReferenceException
的未处理异常,这是代码:
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
switch (fileName)
{
case "":
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
}
这是我的声明:
private string fileName = "";
private Bitmap bitmap;
private Bitmap curBitmap;
这是我的完整代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Bitmap
//Graphics
Graphics g;
Pen p = new Pen(Color.Black, 8);
Point start = new Point(0, 0);
Point end = new Point(0, 0);
bool drawing = false;
//private int x1;
//private int x2;
//private int y1;
//private int y2;
//private int d1;
//private int d2;
private string fileName = "";
private Bitmap bitmap;
private Bitmap curBitmap;
private Size fullSize;
private void btnColorPicker_Click(object sender, EventArgs e)
{
//Get colors from colordialog
DialogResult r = colorDialog1.ShowDialog();
if (r == DialogResult.OK)
{
p.Color = colorDialog1.Color;
}
}
private void PanelDrawing_MouseUp(object sender, MouseEventArgs e)
{
drawing = false;
}
private void PanelDrawing_MouseMove(object sender, MouseEventArgs e)
{
if (drawing && !earaser)
{
p.Width = PenSize.Value;
p.Color = colorDialog1.Color;
end = e.Location;
g = PanelDrawing.CreateGraphics();
g.DrawLine(p, start, end);
PanelDrawing.Cursor = Cursors.HSplit;
}
else if (drawing && earaser)
{
end = e.Location;
g = PanelDrawing.CreateGraphics();
g.DrawLine(p, start, end);
PanelDrawing.Cursor = Cursors.Cross;
}
else if (!drawing)
{
PanelDrawing.Cursor = Cursors.Default;
}
start = end;
}
private void PanelDrawing_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
if (e.Button == MouseButtons.Left)
{
drawing = true;
}
}
private void PenSize_Scroll(object sender, EventArgs e)
{
p.Width = PenSize.Value;
label1.Text = "" + PenSize.Value;
}
bool earaser = false;
private void btnEaraser_Click(object sender, EventArgs e)
{
p.Color = Color.White;
p.Width = 10;
earaser = true;
}
private void btnBrush_Click(object sender, EventArgs e)
{
earaser = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
g.Clear(PanelDrawing.BackColor);
}
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
if (string.IsNullOrWhiteSpace(fileName))
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
else
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
}
private void Form1_Load(object sender, EventArgs e)
{
fullSize = SystemInformation.PrimaryMonitorMaximizedWindowSize;
bitmap = new Bitmap(fullSize.Width, fullSize.Height);
g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(BackColor);
}
}
由于您没有指定 "where",System.NullReferenceException 正在发生。我会假设以下2种情况
1.fileName 为空。如果是这种情况,请在需要 null 的 switch 语句中再添加 1 个 case(如下所示)考虑在内。
switch (fileName)
{
case "":
case null:
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
2。您在保存时使用的是相同的图像流,用于构造它。如果是这种情况,请使用如下所示的新位图对象。
var newBitmap = new Bitmap(bitmap);
switch (fileName)
{
case "":
case null:
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
newBitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
newBitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
首先,我认为 switch 不是处理字符串的好方法。
string.IsNullorEmpty
和 string.IsNullorWhiteSpace
可能对您有用。
你能在控制台上跟踪位图吗?您确定您在代码中正确地声明和初始化了它吗?
这应该可以解决任何可能性:
private void saveToolStripMenuItem_Click(object sender,System.EventArgs e)
{
var _Bitmap = new Bitmap(bitmap);
//when we quit here we don't need this anymore; declaring it here helps with memory management
if(_Bitmap == null)
return;
if(string.IsNullorEmpty(fileName)||string.IsNullorWhiteSpace(fileName)
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
}
else
{
_Bitmap.Save(fileName,ImageFormat.Bmp);
}
}
您在 Form1_Load
上分配位图,尝试在单独的方法上进行,并在需要时调用它。
如果变量为 null 或空字符串,它可能没有捕捉到变量。
所以不要使用 switch 语句,而是尝试在 If-Else 语句中进行。
这不是使用 switch 语句的地方。这更容易重写为 if:
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
if (string.IsNullOrWhiteSpace(fileName))
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
else
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
}
这个开关不合适,因为它非常混乱,你必须测试多种情况(null、空字符串、空格)。您可以堵塞代码并使其工作,但这更简单。
如果不查看其余代码,就无法判断 bitmap
是否为空。如果它为空,则调用 .Save
将导致 NullReferenceException
。了解为什么它为空的最好方法是学习如何使用调试器。在位图或使用位图的位置设置断点,看看为什么不写入位图或首先创建对象。
我有一些关于 savefiledialog c# 的问题,我在调试时遇到类型为 System.NullReferenceException
的未处理异常,这是代码:
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
switch (fileName)
{
case "":
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
}
这是我的声明:
private string fileName = "";
private Bitmap bitmap;
private Bitmap curBitmap;
这是我的完整代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Bitmap
//Graphics
Graphics g;
Pen p = new Pen(Color.Black, 8);
Point start = new Point(0, 0);
Point end = new Point(0, 0);
bool drawing = false;
//private int x1;
//private int x2;
//private int y1;
//private int y2;
//private int d1;
//private int d2;
private string fileName = "";
private Bitmap bitmap;
private Bitmap curBitmap;
private Size fullSize;
private void btnColorPicker_Click(object sender, EventArgs e)
{
//Get colors from colordialog
DialogResult r = colorDialog1.ShowDialog();
if (r == DialogResult.OK)
{
p.Color = colorDialog1.Color;
}
}
private void PanelDrawing_MouseUp(object sender, MouseEventArgs e)
{
drawing = false;
}
private void PanelDrawing_MouseMove(object sender, MouseEventArgs e)
{
if (drawing && !earaser)
{
p.Width = PenSize.Value;
p.Color = colorDialog1.Color;
end = e.Location;
g = PanelDrawing.CreateGraphics();
g.DrawLine(p, start, end);
PanelDrawing.Cursor = Cursors.HSplit;
}
else if (drawing && earaser)
{
end = e.Location;
g = PanelDrawing.CreateGraphics();
g.DrawLine(p, start, end);
PanelDrawing.Cursor = Cursors.Cross;
}
else if (!drawing)
{
PanelDrawing.Cursor = Cursors.Default;
}
start = end;
}
private void PanelDrawing_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
if (e.Button == MouseButtons.Left)
{
drawing = true;
}
}
private void PenSize_Scroll(object sender, EventArgs e)
{
p.Width = PenSize.Value;
label1.Text = "" + PenSize.Value;
}
bool earaser = false;
private void btnEaraser_Click(object sender, EventArgs e)
{
p.Color = Color.White;
p.Width = 10;
earaser = true;
}
private void btnBrush_Click(object sender, EventArgs e)
{
earaser = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
g.Clear(PanelDrawing.BackColor);
}
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
if (string.IsNullOrWhiteSpace(fileName))
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
else
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
}
private void Form1_Load(object sender, EventArgs e)
{
fullSize = SystemInformation.PrimaryMonitorMaximizedWindowSize;
bitmap = new Bitmap(fullSize.Width, fullSize.Height);
g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(BackColor);
}
}
由于您没有指定 "where",System.NullReferenceException 正在发生。我会假设以下2种情况
1.fileName 为空。如果是这种情况,请在需要 null 的 switch 语句中再添加 1 个 case(如下所示)考虑在内。
switch (fileName)
{
case "":
case null:
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
2。您在保存时使用的是相同的图像流,用于构造它。如果是这种情况,请使用如下所示的新位图对象。
var newBitmap = new Bitmap(bitmap);
switch (fileName)
{
case "":
case null:
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = saveFileDialog1.FileName;
newBitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
break;
default:
{
newBitmap.Save(fileName, ImageFormat.Bmp);
}
break;
}
首先,我认为 switch 不是处理字符串的好方法。
string.IsNullorEmpty
和 string.IsNullorWhiteSpace
可能对您有用。
你能在控制台上跟踪位图吗?您确定您在代码中正确地声明和初始化了它吗?
这应该可以解决任何可能性:
private void saveToolStripMenuItem_Click(object sender,System.EventArgs e)
{
var _Bitmap = new Bitmap(bitmap);
//when we quit here we don't need this anymore; declaring it here helps with memory management
if(_Bitmap == null)
return;
if(string.IsNullorEmpty(fileName)||string.IsNullorWhiteSpace(fileName)
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
}
else
{
_Bitmap.Save(fileName,ImageFormat.Bmp);
}
}
您在 Form1_Load
上分配位图,尝试在单独的方法上进行,并在需要时调用它。
如果变量为 null 或空字符串,它可能没有捕捉到变量。
所以不要使用 switch 语句,而是尝试在 If-Else 语句中进行。
这不是使用 switch 语句的地方。这更容易重写为 if:
private void saveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
if (string.IsNullOrWhiteSpace(fileName))
{
saveFileDialog1 = new SaveFileDialog
{
Filter = @"Image files (*.bmp)|*.bmp|All files (*.*)|*.*",
FileName = "MyPicture.bmp"
};
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
fileName = saveFileDialog1.FileName;
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
else
{
bitmap.Save(fileName, ImageFormat.Bmp);
}
}
这个开关不合适,因为它非常混乱,你必须测试多种情况(null、空字符串、空格)。您可以堵塞代码并使其工作,但这更简单。
如果不查看其余代码,就无法判断 bitmap
是否为空。如果它为空,则调用 .Save
将导致 NullReferenceException
。了解为什么它为空的最好方法是学习如何使用调试器。在位图或使用位图的位置设置断点,看看为什么不写入位图或首先创建对象。