开发者

C# saving an image from PictureBox

i have a code like this:

private void Load_Button_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();            
        if (dialog.ShowDialog()==DialogResult.OK){
            MessageBox.Show(dialog.FileName,"My Application", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            string s; 
            s=".bmp";
            if (dialog.FileName.Substring(dialog.FileName.LastIndexOf('.')).Equals(s))
            {
             开发者_开发问答   picBox_1.Load(dialog.FileName);
                BitmapFile = new Bitmap(dialog.FileName.ToString());
            }
            else {
                MessageBox.Show("Not a BMP file!");
            }
        }

    }

so, load image. and have an error in this:

private void Save_Button_Click(object sender, EventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog();
        try
        {
            if (picBox_1.Image != null)
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show(dialog.FileName, "My Application", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    string s;
                    s = ".bmp";
                    if (dialog.FileName.Substring(dialog.FileName.LastIndexOf('.')).Equals(s))
                    {

                        picBox_1.Image.Save(dialog.FileName.ToString());
                        //BitmapFile.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("Not a BMP file!");
                    }
                }
            }
            else
            {
                MessageBox.Show("My PicBox is empty!");
            }
        }
        catch (Exception) { MessageBox.Show("Cannot save file, error!"); }

    }

this is general GDI error. I suppose, that i can't write to file (not enough rights, maybe). how can i improve this error?


you should catch the exceptions properly, not with a MessageBox which tells you nothing about the exact exception thrown!

at minimum your catch block should look like this:

catch (Exception exc)
{
  MessageBox.Show(exc.Message);
}

and I say at minimum because you should in fact log the exception somewhere, using a logging framework like NLog or Log4Net and dump stack trace and other details. You are not even able to tell the excact type of Exception if you show a message with a static string and not the details of the actual exception.


You should only catch specific exceptions that you intend to handle or recover from, and log the details. Never catch Exception as you would potentially be masking bigger issues with your server if they occur.

Unexpected exceptions should bubble up so that the cause can quickly be identified when they occur.

See here for Best Practices for Handling Exceptions.


You're eating the exception and losing all the juicy detail. Try changing your catch block to something like this to see what's going on:

catch (Exception ex)
{
    MessageBox.Show(this, ex.ToString(), "Error Saving Image", MessageBoxIcons.Error);
}

Also, consider implementing some logging (to the event viewer and/or text file. This will allow you to have a simple message box, but with all the juicy detail put somewhere useful to fetch after the event.

catch (Exception ex)
{
    MessageBox.Show(this, ex.Message, "Error Saving Image", MessageBoxIcon.Error);

    // _logger is a private field on this class in this case.
    _logger.Log(ex, string.Format("Saving image to {0}", dialog.Filename))
}

You could look at Log4net amongst other things for the actual logging, but at the very least write a class to write exception detail to the event viewer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜