在 C# 应用程序中使用 OpenFileDialog 控件

发布于 2024-09-09 03:03:21 字数 381 浏览 7 评论 0原文

我确信我以前问过这个问题,但搜索没有任何作用,我完全忘记了如何做到这一点。

我需要一种方法让用户从硬盘驱动器中选择图片并使用该位置将该图片加载到 Image 类中。

我过去曾这样做过,但正如我所说,我不记得我是如何做到的。

我知道您可以将文件类型过滤器应用于 OpenFileDialog。

private void LoadImageToMemory()
        {
            openFileDialog1.Filter = "JPEG | jpeg";
            openFileDialog1.ShowDialog();            
        }

有什么指导吗?谢谢你!

I'm sure I've asked this question before but searching does nothing and I completely forgot how to do this.

I need a way to have a user choose a picture from their hard drive and load that picture to an Image class using the location.

I've done this in the past, but as I said I can't remember how I did it.

I know you can apply a file type filter to the OpenFileDialog.

private void LoadImageToMemory()
        {
            openFileDialog1.Filter = "JPEG | jpeg";
            openFileDialog1.ShowDialog();            
        }

Any guidance? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-09-16 03:03:21

我想通了!

如果有人有同样的问题,这就是你的做法。

private void LoadImageToMemory()
        {
            openFileDialog1.Filter = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Select a picture to transform.";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = openFileDialog1.FileName;
            }            
        }

I figured it out!

In case anyone has the same question, this is how you do it.

private void LoadImageToMemory()
        {
            openFileDialog1.Filter = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Select a picture to transform.";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = openFileDialog1.FileName;
            }            
        }
撩人痒 2024-09-16 03:03:21

您是否尝试阅读手册

OpenFileDialog dlg = new OpenFileDialog();

// Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations 
//           OR Office Files 
//           OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt"

。您确实应该在 MSDN 甚至 Google 上寻找此类琐碎的信息,而不是 Stack Overflow。 MSDN 是您的朋友,是.Net 开发人员的编程圣经。

Did you try reading the manual?

OpenFileDialog dlg = new OpenFileDialog();

// Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations 
//           OR Office Files 
//           OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt"

. You really should be looking for such trivial info on MSDN or even Gooogle, instead of Stack Overflow. MSDN is your friend, THE programming bible for .Net developers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文