开发者

get name of picture?

开发者 https://www.devze.com 2023-02-01 06:10 出处:网络
if(pictureBox4.Image.ToString() == ePRO_Decision_Tool.Properties.Resources.mod_onalertq.ToString())...
if(pictureBox4.Image.ToString() == 
    ePRO_Decision_Tool.Properties.Resources.mod_onalertq.ToString())...

How to read name of image file loaded in pictureBox (or fro开发者_StackOverflow社区m resources)?


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.


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


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


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);
    }
}


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

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


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);


get Image name by click

 String getImageName = pictureBox1.Name;

        MessageBox.Show(getImageName);


            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);
        
        }


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消