c# how to show image in picturebox if file format is .jpg

4.3k views Asked by At

i have a problem. i want to load image file or rar/zip to my WPF. when i click button on my WPF to open file dialog, i got some errors.

this is my code to open file dialog.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        op.Title = "Select a File";
        op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                    "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                    "Portable Network Graphic (*.png)|*.png"+
                    "Zip Files|*.zip;*.rar";

        if (op.ShowDialog() == DialogResult.OK)
        {
                pictureBox1.Image = System.Drawing.Image.FromFile(op.FileName);
                _path = op.FileName;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

my question is how to show image if file format is .jpg / .png, and show rar icon or any icon if file format is .rar / .zip in picturebox.

3

There are 3 answers

1
Ali Akyıldırım On BEST ANSWER

İf I don't understand wrong, the solution for your question is like this code below;

Firstly, you should choose a general picture to show in the picturebox when a zip or rar file is chosen by user. Then place the chosen picture (ex: rar.jpg) under the app's **bin\debug** folder.

Then, use the codes below;

        try
        {

            op.Title = "Select a File";
            op.Filter = "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|"
   + "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
   + "Zip Files|*.zip;*.rar";

            if (op.ShowDialog() == DialogResult.OK)
            {
                string x = op.FileName.ToString();
                char[] ayrac = { '.' };
                string[] kelimeler = x.Split(ayrac);
                string y = kelimeler[1].ToString();

                if (y != "zip" && y != "rar")
                {
                    pictureBox1.Image = System.Drawing.Image.FromFile(op.FileName);
                    _path = op.FileName;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                }
                else
                {
                    //How to get picture: The best way is to put the subfolder under the app's bin\debug\,thus you can simply coding:
                    pictureBox1.Image = Image.FromFile(@"rar.jpg", true);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

I hope, the code is ok for your question.

7
DavidG On

It seems you are trying to get the file icon. To do that use ExtractAssociatedIcon, like this:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
pictureBox1.Image = icon.ToBitmap();
1
Alexander Pacha On

Maybe all you want is a simple if-statement that checks the file-ending, like this:

using System.IO;

if (Path.GetExtension(op.FileName).EndsWith("rar")
    || Path.GetExtension(op.FileName).EndsWith("zip"))
{
    // File has rar or zip extension
    // Load default image from resources
} 
else
{
    // Load provided image
}

and you may even add StringComparison.InvariantCultureIgnoreCase to the EndsWith-call if you want to make it case-insensitive.