Hello all im new to prgramming and ive gotten to a dead end where im hoping some of you people can help me!

Basically i am trying to make a media player that opens pictures audio and video into seperate forms, so i have 4 forms main form where i have used an openFileDialog to select file types, the openFileDialog is excecuted from a menuStrip which has the 3 video pictures and audio options.

What i am stuck on is the pictures part i have managed to get the openFileDialog to open and be flitered to jpeg and bitmaps etc and once a picture file is selected ive managed to open the pictures form where i want the picture to open into a picture box on that particular formmy problem is exactly that trying to get the picture open into that form and into that picturebox.

I have got the following code so far:

private void pictureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String Chosen_File = "";

            ofSelectMedia.InitialDirectory = "C:";
            ofSelectMedia.Title = "Select An Image";
            ofSelectMedia.FileName = "";
            ofSelectMedia.ShowDialog();

            Chosen_File = ofSelectMedia.FileName;
            
            frmPictureViewer frmMediaPlayer = new frmPictureViewer();
            frmMediaPlayer.Show();

Please help! :(

Recommended Answers

All 12 Replies

I don't get you. To load the image opened in openFileDialog and set it as pictureBox's image, you can simpl use:

Image img = Image.FromFile(Chosen_File);
pictureBox1.Image = img;

I don't get you. To load the image opened in openFileDialog and set it as pictureBox's image, you can simpl use:

Image img = Image.FromFile(Chosen_File);
pictureBox1.Image = img;

Thanks for your reply i want the image to displayed in a picturebox on another form not in the same form as the openFileDialog. So when the file is selected ie the image it opens up form2 and displays the image in the pictureBox on that form! Do you get me?

You can pass the file path to the new form in its constructor:

public partial class frmPictureViewer : Form
{
    public frmPictureViewer (string FilePath)
    {
        Image img = Image.FromFile(FilePath);
        pictureBox1.Image = img;
    }
 
}

Then call your new form with frmPictureViewer picViewer = new frmPictureViewer("some file path");

You can pass the file path to the new form in its constructor:

public partial class frmPictureViewer : Form
{
    public frmPictureViewer (string FilePath)
    {
        Image img = Image.FromFile(FilePath);
        pictureBox1.Image = img;
    }
 
}

Then call your new form with frmPictureViewer picViewer = new frmPictureViewer("some file path");

Hi ryshad thanks for your reply and i think i nearly got it 1 more question how do i know whih filepath to put in??

Sorry guys i know this is simple but i am a complete noob! :(

the openFileDialog has a FileName property that stores the path of the selected file. You should check the openFileDialogs DialogResult property to ensure the user didnt cancel as well

Ok i think i got everything but that line of code you wrote last!

frmPictureViewer picViewer = new frmPictureViewer("some file path");

I have not been able to figure out where you wrote "some file path" and when i dont put nothing in there it says something about .frmPictureViewer does not contain a constructer that takes "0" arguments..????

Im really confused guys!

Thank you so much for helping me so far! Really Appreicate it!

Ok, the custom constructor i showed you replaced the default one.
The standard constructor for a form has zero parameters:

public frmPictureViewer ()
    {

    }

The constructor i showed you has one parameter, of type string, called FilePath:

public frmPictureViewer ([U]string FilePath[/U])
    {

You have to pass a string value containing the file path when you call the constructor. I wrote "some file path" to show you should put a string containing a file path. Get the file path from your open file dialog object and send that with the constructor.

Also, i didnt want to add any code beyond what was needed for the picturebox, but you need to include the following line in the constructor to create the controls before you access them:

public frmPictureViewer (string FilePath)
    {
        InitializeComponents();
        //picture box code goes here
    }

Right this is what i have got so far..
For the frmMediaPlayer

private void pictureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String Chosen_File = "";
            Chosen_File = ofSelectMedia.FileName;

            ofSelectMedia.InitialDirectory = "C:";
            ofSelectMedia.Title = "Please Select An Image";
            ofSelectMedia.FileName = "";
            ofSelectMedia.ShowDialog();

            frmPictureViewer frmMediaPlayer = new frmPictureViewer(Chosen_File);
            frmMediaPlayer.Show();

and for the frmPictureViewer..

{
    public partial class frmPictureViewer : Form
    {
        public string Chosen_File { get; set; }
        public frmPictureViewer(string Chosen_File)
        {
            InitializeComponent();
            this.Chosen_File = Chosen_File;
            picBox1.Image = Image.FromFile(Chosen_File);
        }

What am i missing?

You are accessing the ofSelectMedia.FileName before you show the dialog and allow the user to select a file.

Everything else seems ok. Just a note, you have made the Chosen_File string a public property. If you want to use a property you can set the image that way without using the constructor.

Did everything you said still getting an error for this line of code

Image img = Image.FromFile(Chosen_File);

Says the path is not of a legal form???

have you debugged your code to check what Chosen_File contains when that line of code executes?

Dont worry lads i worked my way around thanks a lot for your helpo really appreciate it! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.