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.
İ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.