Posted By

padapara on 04/12/17


Tagged


Versions (?)

Code source pour un OpenFileDialog en C# filtrant toutes les extensions image dans un seul groupe... ou pas !


 / Published in: C#
 

This code enable you to have an OpenFileDialog that select directly all types of images.

  1. OpenFileDialog dlg = new OpenFileDialog();
  2. string filter = "Tous les fichiers image|";
  3.  
  4. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  5. string sep = "";
  6.  
  7. bool isFirst = true;
  8. foreach (var c in codecs)
  9. {
  10. if (!isFirst)
  11. sep = ";";
  12.  
  13. filter = String.Format("{0}{1}{2}", filter, sep, c.FilenameExtension);
  14. isFirst = false;
  15. }
  16.  
  17. filter += "|";
  18.  
  19. sep = string.Empty;
  20.  
  21. foreach (var c in codecs)
  22. {
  23. string codecName = c.CodecName.Substring(8).Replace("Codec", "").Trim();
  24. filter = String.Format("{0}{1}{2} ({3})|{3}", filter, sep, codecName, c.FilenameExtension);
  25. sep = "|";
  26. }
  27.  
  28. dlg.Filter = filter;
  29.  
  30. Nullable<bool> result = dlg.ShowDialog();
  31.  
  32. if (result == true)
  33. {
  34. imageUri = dlg.FileName;
  35. }

Report this snippet  

You need to login to post a comment.