0

我正在尝试使用 openFileDialog 打开位图图像并将其放在我的表单上。我的表单构造器...

 public Form1()
    {
        InitializeComponent();
        drawing = new Bitmap(drawingPanel.Width, drawingPanel.Height, drawingPanel.CreateGraphics());
        Graphics.FromImage(drawing).Clear(Color.White);

        // set default value for line thickness
        tbThickness.Text = "5";
    }

...打开一个带有空白屏幕的新表单,我可以使用鼠标和各种颜色选择器按钮在其上绘图。然后我用这种方法保存文件:

private void btnSave_Click(object sender, EventArgs e)
    {
        // save drawing
        if (file == null)   // file is a FileInfo object that I want to use
                            // to check to see if the file already exists 
                            // I haven't worked that out yet
        {
            drawing.Save("test.bmp");
            //SaveBitmap saveForm = new SaveBitmap();
            //saveForm.Show();
        }
        else
        {
            drawing.Save(fi.FullName);
        }
    }

该图像确实以 .bmp 文件的形式保存到调试文件夹中。然后我使用 OpenFileDialog 打开文件:

private void btnOpen_Click(object sender, EventArgs e)
    {
        FileStream myStream;
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Filter = "bmp files (*.bmp)|*.bmp";

        if (openFile.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = (FileStream)openFile.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        PictureBox picBox = new PictureBox();
                        picBox.Location = drawingPanel.Location;
                        picBox.Size = drawingPanel.Size;
                        picBox.Image = new Bitmap(openFile.FileName);
                        this.Controls.Add(picBox);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }

不幸的是 OpenFileDialog 对话框出现了。当我选择文件 test.bmp 时,屏幕消失然后重新出现,当我再次选择它时,OpenFileDialog 窗口消失,我回到没有图像的表单。希望得到一些指点。没有编译或运行时错误。

4

3 回答 3

0

您打ShowDialogue了两次电话,这可能是您的问题的根源。只需使用以下代码,从方法中删除所有其他内容。您的使用using也不正确。它确实清理了正在处理结果的内容。您需要重构或删除 using 语句。

private void btnOpen_Click(object sender, EventArgs e)
{
     OpenFileDialog dlg = new OpenFileDialog()
     {
            dlg.Title = "Open Image";
            dlg.Filter = "bmp files (*.bmp)|*.bmp";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                PictureBox picBox = new PictureBox();
                picBox.Location = drawingPanel.Location;
                picBox.Size = drawingPanel.Size;
                picBox.Image = new Bitmap (dlg.FileName);
                this.Controls.Add(picBox);
            }
      }
  }

上面的代码有效,但没有清理或错误处理。我会把它留给你。

于 2013-03-01T19:36:58.547 回答
0

为什么要打ShowDialog()两次电话?

只需调用ShowDialog一次,因此它不会像您指示的那样打开两次。

来自MSDN

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
                PictureBox picBox = new PictureBox();
                picBox.Location = drawingPanel.Location;
                picBox.Size = drawingPanel.Size;
                picBox.Image = new Bitmap (myStream);
                this.Controls.Add(picBox);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }
}
于 2013-03-01T19:34:45.327 回答
0

您打开一个对话框面板,然后在它关闭时检查结果是否正常;然后在块中打开另一个新对话框using;然后你将它的图像结果分配给,然后在块处理PictureBox时扔掉所有东西。using

于 2013-03-01T19:35:39.480 回答