保存文件对话框未处理的异常

时间:2015-05-08 00:59:55

标签: c# savefiledialog

我有一些关于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);
    }


}

4 个答案:

答案 0 :(得分:1)

由于您尚未指定 System.NullReferenceException 正在发生的“where”。我将假设以下2个案例

1.fileName为空。如果是这种情况,请将 case (如下所示)添加到 switch 语句中,并将 null 考虑在内。

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;
        }

答案 1 :(得分:1)

可能它没有捕获变量,如果它的null或空字符串。

所以不要使用switch语句,而是尝试在If-Else语句中执行。

答案 2 :(得分:1)

这不是使用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是否为空。如果为null,则调用.Save将导致NullReferenceException。了解其null的最佳方法是学习如何使用调试器。在位图或利用位图的位置设置断点,以查看为什么不写入或首先创建对象。

答案 3 :(得分:0)

首先,切换不是我认为处理字符串的好方法。

&#xA;&#xA;

string.IsNullorEmpty 字符串。 IsNullorWhiteSpace 可能对您有用。

&#xA;&#xA;

您可以在控制台上跟踪位图吗?您确定在代码中正确声明并初始化了吗?

&#xA;&#xA;

这应解决任何可能性:

&#xA;&#xA;
 < code> private void saveToolStripMenuItem_Click(object sender,System.EventArgs e)&#xA; {&#xA; var _Bitmap = new Bitmap(bitmap);&#xA; //当我们退出这里时,我们不再需要这个了;在这里声明它有助于内存管理&#xA; if(_Bitmap == null)&#xA;返回;&#XA;&#XA; if(string.IsNullorEmpty(fileName)|| string.IsNullorWhiteSpace(fileName)&#xA; {&#xA; Filter = @“图像文件(* .bmp)| * .bmp |所有文件(*。*)| * 。*“,&#xA; FileName =”MyPicture.bmp“&#xA;}&#xA; else&#xA; {&#xA; _Bitmap.Save(fileName,ImageFormat.Bmp);&#xA;}& #xA;}&#xA;  
&#xA;&#xA;

您在 Form1_Load 上分配位图,尝试在单独的方法上执行并调用它当你需要它时。

&#xA;