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

前端 未结 11 1014
陌清茗
陌清茗 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:31

    Here's an example of the ImageCodecInfo suggestion (in VB):

       Imports System.Drawing.Imaging
            ...            
    
            Dim ofd as new OpenFileDialog()
            ofd.Filter = ""
            Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
            Dim sep As String = String.Empty
            For Each c As ImageCodecInfo In codecs
                Dim codecName As String = c.CodecName.Substring(8).Replace("Codec", "Files").Trim()
                ofd.Filter = String.Format("{0}{1}{2} ({3})|{3}", ofd.Filter, sep, codecName, c.FilenameExtension)
                sep = "|"
            Next
            ofd.Filter = String.Format("{0}{1}{2} ({3})|{3}", ofd.Filter, sep, "All Files", "*.*")
    

    And it looks like this:

    enter image description here

提交回复
热议问题