Winforms C++;-将图像从窗体保存到文件

Winforms C++;-将图像从窗体保存到文件,winforms,visual-c++,c++-cli,Winforms,Visual C++,C++ Cli,我是Windows窗体应用程序编程新手。我想捕获我的Windows窗体(使用函数CaptureScreen())并通过使用SaveFileDialog单击按钮将其保存到文件中。这是我的代码: private: System::Void CaptureScreen() { Drawing::Graphics^ myGraphics = this->CreateGraphics(); memoryImage = gcnew Drawing::Bitmap(Size.Width

我是Windows窗体应用程序编程新手。我想捕获我的Windows窗体(使用函数CaptureScreen())并通过使用SaveFileDialog单击按钮将其保存到文件中。这是我的代码:

private: System::Void CaptureScreen() 
{
    Drawing::Graphics^ myGraphics = this->CreateGraphics();
    memoryImage = gcnew Drawing::Bitmap(Size.Width, Size.Height, myGraphics);
    Drawing::Graphics^ memoryGraphics = Drawing::Graphics::FromImage(memoryImage);
    memoryGraphics->CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, this->Size);
}

private: System::Void btnSave_Click(System::Object^  sender, System::EventArgs^  e) 
{
    SaveFileDialog^ saveDiag2 = gcnew SaveFileDialog();
    saveDiag2->Filter = "Dateityp PNG (*.PNG)|*.PNG|All files (*.*)|*.*";
    saveDiag2->FilterIndex = 1;
    saveDiag2->RestoreDirectory = true;
    if (saveDiag2->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {
        String^ savePath = saveDiag2->FileName;
        if (saveDiag2->OpenFile() != nullptr)
        {
            try
            { 
                CaptureScreen();
                if (memoryImage != nullptr)
                {
                    memoryImage->Save(savePath, Imaging::ImageFormat::Png);
                }
            }
            catch (Exception^)
            {
                MessageBox::Show("There was a problem saving the file."
                    "Check the file permissions.");
            }
        }
    }
}

我总是显示异常。我做错了什么?汉斯·帕桑是对的。您应该处理IDisposable对象。在C++ CLI中,可以借助于MSCLR::AutoMa句柄智能指针。
#include <msclr/auto_handle.h>

private: System::Void CaptureScreen() 
         {
             msclr::auto_handle<Drawing::Graphics> myGraphics(this->CreateGraphics());
             memoryImage = gcnew Drawing::Bitmap(Size.Width, Size.Height, myGraphics.get());
             msclr::auto_handle<Drawing::Graphics> memoryGraphics(Drawing::Graphics::FromImage(memoryImage));
             memoryGraphics->CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, this->Size);
         }

C++-CLI
是此代码的正确标记,也为Windows窗体添加了标记。如果您能显示异常内容,它可能会指示您出了什么问题。您还可以在try块的开头放置一个断点进行调试,这是一个非常糟糕的错误处理代码,永远不会显示“it not work”消息。这对任何人都没有帮助,对你也没有。使用异常对象的ToString()方法。您的代码丢失
删除memoryImage如果不这样做,则文件将保持锁定状态,尝试覆盖它将失败。感谢您的回答。我在@Hans Passant建议的错误处理代码之后添加了
delete memoryImage
@Dmitry异常对象现在产生以下错误:
System.Runtime.InteropServices.ExternalException(0x80004005):GDI+中的错误。
在System.Drawing.Image.Save(字符串文件名,ImageCodeInfo编码器,编码器参数EncoderParameters encoderParams)
在System.Drawing.Image.Save(字符串文件名,Imageformat格式)
在程序中的btnSave\u单击(对象发送方,事件参数e)。h:第548行。
是由于保存在网络文件夹中而发生的错误。在这种情况下,我应该如何处理它?您可以通过保存到本地驱动器轻松地进行检查。您还可以检查权限。再看看第二个答案。
    catch (Exception^ exception)
    {
        MessageBox::Show("There was a problem saving the file: " + exception->ToString(),
            "Check the file permissions.");
    }