如何验证PictureBox控件仅接受图像文件? [英] How to validate a PictureBox control to accept only image files ?

查看:168
本文介绍了如何验证PictureBox控件仅接受图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个PictureBox控件和一个浏览按钮.
我希望用户浏览时,他应该只能浏览图像文件(所有格式的.jpg,.bmp等),这意味着我想验证PictureBox控件是否仅接受图像文件,否则应该显示一个MessageBox.显示这不是图像文件".

请帮帮我.

There is a PictureBox control and a browse button below it.
I want that when a user browses, he should be able to browse only image files(of all formats .jpg, .bmp, etc.) that means I want to validate my PictureBox control to accept only image files otherwise it should display a MessageBox displaying "This is not an Image File".

Please help me out.

推荐答案

希望您正在使用FileDialog进行浏览.如果是的话,这就像将FileDialog的过滤器设置为仅允许所需的类型一样简单.参见此处: C#文件对话框过滤器 [ ^ ].

或者,如果您具有任何类型的文件路径,则可以使用System.IO中的Path类并使用GetExtension方法进行检查.参见此处: C#路径类 [ ^ ]
Hopefully, you''re using a FileDialog to do your browsing. If you are, it''s as simple as setting the filters for the FileDialog to only allow the types you want. See here: C# filedialog filter[^].

Alternatively, if you have any kind of file path, you can use the Path class in System.IO and use the GetExtension method to check it. See here: C# path class[^]


这篇关于如何验证PictureBox控件仅接受图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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