简体   繁体   English

自定义过滤OpenFileDialog

[英]Custom filtering OpenFileDialog

I am trying to make a program that will allow a user to view an icon in a PictureBox. 我正在尝试制作一个允许用户在PictureBox中查看图标的程序。 I want the user to only be able to open images that are 24x24 pixels. 我希望用户只能打开24x24像素的图像。

I would like to put a filter in an OpenFileDialog to only show images that are 24x24. 我想在OpenFileDialog中放置一个过滤器,只显示24x24的图像。 Is there any way to do this? 有没有办法做到这一点? I heard that it may be possible by customizing the OpenFileDialog and using P/Invoke. 我听说可以通过自定义OpenFileDialog并使用P / Invoke来实现。

You could check the Width and Height of the image: 您可以检查图像的WidthHeight

// 'image' is the image you want to check
if(image.Width > 24 || image.Height > 24)
    MessageBox.Show("Please select a smaller image!");
else
    // This code will always run if the image is smaller than 24x24

Hope this helps! 希望这可以帮助!

If you're reading it in by storing it as an object (which I assume you are), you just need to read off imageObject.Width "using System.Drawing;" 如果你通过将它存储为一个对象(我假设你是)来读它,你只需要使用System.Drawing读取imageObject.Width; or "using System.Drawing.Image;" 或“使用System.Drawing.Image;”

examples here and here . 这里这里的例子。

You can't do this with the OpenFileDialog. 你不能用OpenFileDialog做到这一点。 You would need to write your own dialog that would interrogate the files in each folder and determine if they match your criteria and then show only those files. 您需要编写自己的对话框,询问每个文件夹中的文件,并确定它们是否符合您的条件,然后仅显示这些文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM