vendredi 25 juin 2021

How to filter 'If' Statements for File Types (C#) [duplicate]

I have developed my program to use an OpenFileDialog, which then that displays the image selected in a picture box. However, let's say I have 2 pictureboxes, meant for 2 different file types. How do I have the program detect the different file types after the file filter shown, and open it in the different pictureboxes.

For example, the File Filter Searches for file types that are specified in the Image Formats string, but how do I make it so that executes different 'If' Statements for different File Extensions it detects.

(Image Formats string filters out .jpeg and .png files, but how to make different if statements for different file types detected?)

//File Filter System 
        private void BtnMediaPlayer_Click(object sender, EventArgs e)
        {
            // FFS Activation
            var open = new OpenFileDialog();
            open.Filter = OpenFileImageFilter;

            if (open.ShowDialog() == DialogResult.OK)
            {
             
                // IS IT POSSIBLE to have 1 IF Statement for .jpeg, and 1 IF Statement for .png?

                if (IsValidImageFile(open.FileName))
                {
                    // display image in picture box  
                    picturebox1.Image = new Bitmap(open.FileName);
                    
                }

                else
                {
                    MessageBox.Show("Invalid file type selected.");
                }
            }
        }
        private string[] ImageFormats => new[] { ".jpg", ".jpeg", ".png", ".gif" };

        private string OpenFileImageFilter
        {
            get
            {
                string fileExts = "";
                foreach (string s in ImageFormats)
                {
                    fileExts += $"*{s};";
                }
                return $"Image Files({fileExts})|{fileExts}";
            }

        }

        // Utilizies ImagesFormat to Check File Extension
        private bool IsValidImageFile(string filename)
        {
            if (string.IsNullOrEmpty(filename)) return false;
            string extension = Path.GetExtension(filename);
            return ImageFormats.Contains(extension);
        }
        ///

Aucun commentaire:

Enregistrer un commentaire