C ++ picturebox图像下载 [英] C++ picturebox image download

查看:79
本文介绍了C ++ picturebox图像下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试使用OpenFileDialog()方法将图像添加到PictureBox中。



我是什么尝试过:



Hello,
I'm trying to add an image into PictureBox with OpenFileDialog() method.

What I have tried:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    OpenFileDialog^ ofd = gcnew OpenFileDialog;
    ofd->InitialDirectory = "C:\\";
    ofd->Filter =
        "Images (*.bmp;*.gif;*.jpg;*.jpeg;*.png)|"
        "*.bmp;*.gif;*.jpg;*.jpeg;*.png|"
        "Bitmaps (*.bmp)|*.bmp|"
        "GIFs (*.gif)|*.gif|"
        "JPEGs (*.jpg)|*.jpg;*.jpeg|"
        "PNGs (*.png)|*.png";
    ofd->FilterIndex = 1;
    if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
            pictureBox11->Image = Image::FromFile(ofd->FileName);
            pictureBox11->SizeMode = PictureBoxSizeMode::StretchImage;
        }
    else {
        MessageBox::Show("Can't open the file. Wrong file type", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::No;
    }
    }



但是当我点击此按钮时,下一条消息显示:

'系统。 Threading.ThreadStateException:'在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保您的Main函数标记了STAThreadAttribute。只有在调试器附加到进程时才会引发此异常。'

推荐答案

由于您实际上没有发布调试器,我将假设您的问题你是怎么做的那个消息。答案在消息中给出。
Since you haven't actually posted one, I will assume your question is what do you do about that message. The answer is given in the message.
Quote:

确保你的Main函数上标有STAThreadAttribute

Ensure that your Main function has STAThreadAttribute marked on it

如果那不是你以后那么你应该考虑实际问一个问题。

If that is not what you are after then you should consider actually asking a question.


当您阅读 STAThreadAttribute 你会读到:

你正在开发一个Windows Forms应用程序。如果Windows Forms应用程序与Windows系统通信,它们必须是单线程的组件,如剪贴板或Windows常用对话框,或者如果他们使用系统功能,如拖放功能



什么意味着你需要在项目设置中设置它以使用公共文件对话框。这些问题被称为:按设计工作
When your read the documentation of STAThreadAttribute from Microsoft you would have read that:
"You're developing a Windows Forms app. Windows Forms apps must be single-threaded if they communicate with Windows system components such as the Clipboard or Windows common dialog boxes, or if they use system features such as drag-and-drop functionality"

What means that you need to set that in the project settings to use the Common File Dialog. Such problems are knowns as: "Works as designed"


这篇关于C ++ picturebox图像下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆