I'm trying to create a movie ticketing system in c# and if the user selects a movie in the combo box the poster of the movie should be displayed in the picture box. I tried this code but my problem is the choices in the combo box are the path of the image the choices should be the movie title not the path of the image.
private void Form1_Load(object sender, EventArgs e)
{
string[] imgs = Directory.GetFiles(@"C:\Users\me\Documents\C# program\Poster");
foreach(string file in imgs)
{
comboBox1.Items.Add(file);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = comboBox1.SelectedItem.ToString();
pictureBox1.Image = new Bitmap(s);
How do I display the movie poster using combo box with their movie title?
CodePudding user response:
Try this one:
pictureBox1.Image = Image.FromFile(YourImagePathInStringMode);
CodePudding user response:
If I'm reading your question correctly, right now you have a pictureBox that correctly displays the image of a poster chosen from a comboBox, and the problem you have is that you want the contents of the comboBox to be the title of the movie rather than the location of the poster.
Depending on how your program is set up, the easiest way to answer this is probably to save the string array from your Form1_Load function, create a list of movie titles, and use that as your source for the comboBox Items.
If you are using a pre-defined set of movie titles this should be easy, but if might be tricky if you don't know what the movies are going to be ahead of time- if possible, you could use them as the poster image filenames, and have something like this for your Form1_Load function:
private string[] posterPaths;
private void Form1_Load(object sender, EventArgs e)
{
posterPaths = Directory.GetFiles(@"C:\Users\me\Documents\C#program\Poster");
foreach(string file in posterPaths)
{
//find where the name of the image is in the path
int index = file.LastIndexOf('\');
//remove the parts that aren't the movie's name
string movieName = file.Substring(index,file.Length-index-4)
comboBox1.Items.Add(movieName);
}
}
You could then use the comboBox.SelectedIndex
property to figure out which path to use for the imageBox.
CodePudding user response:
Define a image path globally,
private const string imagePath = @"C:\Users\sachithw\Desktop\bikes";
But it's more configurable by adding App.Config
file and read the path from there.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="imagePath" value="C:\Users\sachithw\Desktop\bikes" />
</appSettings>
</configuration>
and you can read the path as follows
string imagePath = System.Configuration.ConfigurationManager.AppSettings["imagePath"].ToString();
Full Code,
public partial class Form1: Form {
private
const string imagePath = @ "C:\Users\sachithw\Desktop\bikes";
//string imagePath = System.Configuration.ConfigurationManager.AppSettings["imagePath"].ToString();
public Form1() {
this.BackgroundImageLayout = ImageLayout.Stretch;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
DirectoryInfo di = new DirectoryInfo(imagePath);
FileInfo[] files = di.GetFiles("*.jpg");
foreach(FileInfo file in files) {
comboBox1.Items.Add(file.Name);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
string fileName = comboBox1.SelectedItem.ToString();
if (pictureBox1.Image != null && pictureBox1 != null) {
pictureBox1.Image.Dispose();
}
string imageFullPath = imagePath "\\" fileName;
pictureBox1.Image = Image.FromFile(imageFullPath);
}
}
make sure to dispose of the previous picture in the picture box since you are using multiple images in the PictureBox. Otherwise, it will throw the 'System.OutOfMemoryException'
excepition.