ilija veselica
ilija veselica

Reputation: 9574

get name of picture?

if(pictureBox4.Image.ToString() == 
    ePRO_Decision_Tool.Properties.Resources.mod_onalertq.ToString())...

How to read name of image file loaded in pictureBox (or from resources)?

Upvotes: 7

Views: 31233

Answers (9)

Philmon Mpaga
Philmon Mpaga

Reputation: 1

            OpenFileDialog open =  new OpenFileDialog();
        open.Filter = "Image Files(* .jpg; * .jpeg; * .png;)|* .jpg; * .jpeg; * .png;";
        if(open.ShowDialog() == DialogResult.OK){
            
            piturebox1.Image = new Bitmap(open.FileName);
            String ImageName = Path.GetFileName(open.FileName);
        
        }

Upvotes: 0

Salman Ibrahim
Salman Ibrahim

Reputation: 1

get Image name by click

 String getImageName = pictureBox1.Name;

        MessageBox.Show(getImageName);

Upvotes: 0

programer
programer

Reputation: 11

You can use this way to get name of picture in picture box:

System.IO.Path.GetFileName(PictureBox.ImageLocation);

Upvotes: 1

André Abboud
André Abboud

Reputation: 2040

here is a simple way to get image name from picture box in c#:

string imgPath = pictureBox1.ImageLocation; 
string nameImage =imgPath.Substring(imgPath.LastIndexOf('\\')+1);

Upvotes: -1

gajendra
gajendra

Reputation: 11

private void button1_Click(object sender, EventArgs e)
{
    openFileDialog1.FileName = "";
    openFileDialog1.Title = "Images";
    openFileDialog1.Filter = "JPG Image(*.jpg)|*.jpg|BMP Image(*.bmp)|*.bmp";
    openFileDialog1.ShowDialog();
    if (openFileDialog1.FileName.ToString() != "")
    {
        string imagePath = openFileDialog1.FileName.ToString();
        string imagepath = imagePath.ToString();
        imagepath = imagepath.Substring(imagepath.LastIndexOf("\\"));
        imagepath = imagepath.Remove(0, 1);
    }
}

Upvotes: 1

Moory Pc
Moory Pc

Reputation: 910

this Method just working with loading image by PictureBox.Image.Load(Image Path)

not working with load image directly from resource not working with load image by PictureBoc.Image = Image.FromFile(Image Path) because above methods(except Image.Load()) making Image.ImageLocation set to null

PictureBox.Image.Load("Image Path");
string imagepath = PictureBox.Image.ImageLocation.ToString();
                  imagepath = imagepath.Substring(imagepath.LastIndexOf("\\"));
                  imagepath = imagepath.Remove(0, 1);

Upvotes: 0

Yochai Timmer
Yochai Timmer

Reputation: 49271

An Image object only contains the image's binary data. You can manually set the Tag property of the Image to contain the file name (After you've created the image).

If you load an image to the PictureBox using the Load() method, that will update the PictureBox's ImageLocation property to the file's path.

Then you can use pictureBox4.ImageLocation for comparison.

ImageLocation on MSDN

Upvotes: 5

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

The image loaded in PictureBox is just an array of bytes, so to find out what is the filename you must fill the Tag property of PictureBox when any image loaded in it.

Upvotes: 11

fejesjoco
fejesjoco

Reputation: 11903

I'm pretty sure there's no way, the Image class doesn't expose where it came from.

Upvotes: 1

Related Questions