C#从PictureBox保存图像

时间:2021-10-19 00:24:40

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?

这是一般的GDI错误。我想,我不能写入文件(可能没有足够的权利)。我怎样才能改善这个错误?

3 个解决方案

#1


1  

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

你应该正确地捕获异常,而不是使用MessageBox,它不会告诉你抛出的确切异常!

at minimum your catch block should look like this:

至少你的catch块应该是这样的:

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.

我说至少是因为你实际上应该在某个地方记录异常,使用NLog或Log4Net等日志框架以及转储堆栈跟踪和其他细节。如果您显示带有静态字符串的消息而不是实际异常的详细信息,您甚至无法告诉精确类型的Exception。

#2


1  

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.

请参阅此处了解处理异常的最佳实践。

#3


0  

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块更改为这样的内容,看看发生了什么:

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.

您可以查看Log4net以及其他用于实际日志记录的内容,但至少要编写一个类来向事件查看器写入异常详细信息。

#1


1  

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

你应该正确地捕获异常,而不是使用MessageBox,它不会告诉你抛出的确切异常!

at minimum your catch block should look like this:

至少你的catch块应该是这样的:

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.

我说至少是因为你实际上应该在某个地方记录异常,使用NLog或Log4Net等日志框架以及转储堆栈跟踪和其他细节。如果您显示带有静态字符串的消息而不是实际异常的详细信息,您甚至无法告诉精确类型的Exception。

#2


1  

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.

请参阅此处了解处理异常的最佳实践。

#3


0  

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块更改为这样的内容,看看发生了什么:

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.

您可以查看Log4net以及其他用于实际日志记录的内容,但至少要编写一个类来向事件查看器写入异常详细信息。