none
About the picture box image location in C# RRS feed

  • Question

  • Below is the way I always use to show picture image:

    pictureBox3.Image = new Bitmap(" C:\\Users\\Green LED.png");

    That means the user will have to always put the image in that specific location.

    Is there a way the program can detect the image file no matter where the user put?

    Friday, August 3, 2012 9:06 AM

Answers

  • If you would like to display always the same image, and don't want to let the user change it, then you can add them to the solution directory or you can add them as resources also.
    Friday, August 3, 2012 9:29 AM
  • If i understood that correctly, you want to display a user-specified image, right?

    I'd say why dont you try using OpenFileDialog class and let the user browse for that image?

    Try this:

    OpenFileDialog ImageBrowseDialog = new OpenFileDialog();
                ImageBrowseDialog.Filter = "Bitmap Image|*.bmp"; //Only allow files with the specified extension.
                if (ImageBrowseDialog.ShowDialog() == DialogResult.OK)
                {
    //Enters only if user selects a file and clicks ok.
                    pictureBox1.Image = new Bitmap(ImageBrowseDialog.FileName);
    
                }

    Hope that helps.

    EDIT: You need to add using System.IO; for accessing OpenFileDialog class.
    Saturday, August 4, 2012 7:19 PM

All replies

  • Hi,

    What are you doing? What kind of image would you like to display?

    If it is the part of the UI, it should be part of the solution also. If it is something dynamic image, you should get to know the url somehow. Ask the user to browse it, save the url in advance if it is a previously processed image, etc.

    Friday, August 3, 2012 9:10 AM
  • Its a image file which saved on my pc. U mean I have to put the image into the solution's folder?
    Friday, August 3, 2012 9:19 AM
  • If you would like to display always the same image, and don't want to let the user change it, then you can add them to the solution directory or you can add them as resources also.
    Friday, August 3, 2012 9:29 AM
  • If i understood that correctly, you want to display a user-specified image, right?

    I'd say why dont you try using OpenFileDialog class and let the user browse for that image?

    Try this:

    OpenFileDialog ImageBrowseDialog = new OpenFileDialog();
                ImageBrowseDialog.Filter = "Bitmap Image|*.bmp"; //Only allow files with the specified extension.
                if (ImageBrowseDialog.ShowDialog() == DialogResult.OK)
                {
    //Enters only if user selects a file and clicks ok.
                    pictureBox1.Image = new Bitmap(ImageBrowseDialog.FileName);
    
                }

    Hope that helps.

    EDIT: You need to add using System.IO; for accessing OpenFileDialog class.
    Saturday, August 4, 2012 7:19 PM