Setting the filter to an OpenFileDialog to allow the typical image formats?

前端 未结 11 1029
陌清茗
陌清茗 2020-12-22 16:07

I have this code, how can I allow it to accept all typical image formats? PNG, JPEG, JPG, GIF?

Here\'s what I have so far:

public void EncryptFile()
         


        
11条回答
  •  佛祖请我去吃肉
    2020-12-22 16:47

    Just a necrocomment for using string.Join and LINQ.

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    dlgOpenMockImage.Filter = string.Format("{0}| All image files ({1})|{1}|All files|*", 
        string.Join("|", codecs.Select(codec => 
        string.Format("{0} ({1})|{1}", codec.CodecName, codec.FilenameExtension)).ToArray()),
        string.Join(";", codecs.Select(codec => codec.FilenameExtension).ToArray()));
    

提交回复
热议问题