当ImageLocation不起作用时,如何在C#中获取PictureBox的imageURL?

发布于 2024-11-30 12:10:14 字数 137 浏览 2 评论 0原文

当ImageLocation不起作用时,如何获取windows窗体中图片框的图像URL?

string filepath = picturebox.ImageLocation; // Returns null

How to get the image URL of a picture box in windows form when ImageLocation doesn't work?

string filepath = picturebox.ImageLocation; // Returns null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

完美的未来在梦里 2024-12-07 12:10:14

如果您使用 picturebox 的 ImageLocation 属性来加载图像,那么您就会得到您想要的。否则,如果您通过 Image 属性加载它,那么您将不会从 ImageLocation 获取,也不会再次从 Image 获取。

If you use the ImageLocation property of picturebox to load the image, then you get what you want. Other wise if you load it via Image property then you wont get from ImageLocation and neither from Image again.

翻身的咸鱼 2024-12-07 12:10:14

图像位置

PictureBox.ImageLocation

ImageLocation

PictureBox.ImageLocation
2024-12-07 12:10:14

听起来你需要:

Picturebox.ImageLocation

Sounds like you need:

Picturebox.ImageLocation
半边脸i 2024-12-07 12:10:14

最后我找到了解决方案。当我们选择图像时,将Image的路径存储在图片框的Tag属性中。

pictureBox2.Image = new Bitmap(oOpenFileDialog.FileName);
pictureBox2.Tag = oOpenFileDialog.FileName;

并在需要时检索它:

string Path = pictureBox2.Tag + string.Empty;

请记住,您必须在清除时将 Tag 属性设置为 null

Finally I found a solution. Store the path of the Image in the Tag property of the picture box, when we select the image.

pictureBox2.Image = new Bitmap(oOpenFileDialog.FileName);
pictureBox2.Tag = oOpenFileDialog.FileName;

And retrive it when you need it:

string Path = pictureBox2.Tag + string.Empty;

Remember, you have to set the Tag property to null on clearance.

够钟 2024-12-07 12:10:14

将图像分配给 PictureBox 需要遵循一定的方法才能正确完成。
我相信阅读此答案后您会理解使用 PictureBox.ImageLocation 背后的原因和逻辑。

当你得到一个 Image.FromFile;您将需要指定一个字符串,其中包含文件:所选图像的目录、文件名和扩展名。
因此,一旦您选择/加载要加载的图像(假设您将使用 OpenFileDialog);您必须首先将文件完整路径设置为 PictureBox.ImageLocation 属性(这样您就可以在需要时检索它);然后(最好)您可以将图像加载到 PictureBox 中。

请参见下面的示例:

   // Dispose PictureBox Image (Prepare it to load another Image).
    private void DisposeImage()
    {
        if (pictureBox.Image != null)
        {
            pictureBox.Image.Dispose();
            pictureBox.Image = null;
            pictureBox.ImageLocation = null;
            pictureBox.Update();
        }
    }
    

   // 1. Make sure that no Image is Loaded before Loading a new Picture.
   // 2. Set the Image Location to PictureBox.ImageLocation
   // 3. Load the Image.FromFile (Get the string from stored value).
   private void SetImage(string imageFile)
   {
       // Clear any existant Image in PictureBox.
       // [Defensive programming to prevent exceptions]
       DisposeImage();
    
       // Check if full path is valid (File Exists).
       if File.Exists(imageFile))
       {
           // Store the Image location to variable.
           pictureBox.ImageLocation = imageFile;
           
           // Load the Image from stored variable.
           pictureBox.Image = Image.FromFile(pictureBox.ImageLocation);
       }

       // Set an Image (representing "no image"); in case the Selected Image Does not exist or fails to load.
       else { pictureBox.Image = Properties.Resources.NoImageAvailable; }

       // Place your Image Configuration to Adjust to PictureBox Here.
       pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
    }
   

如果您需要获取 PictureBox 图像位置:

    // Retrieve Current PictureBox Image Location (if any).
    private string GetImageLocation()
    {
        // Set the default Image Location value (empty).
        // This is usefull to check wether the string is empty or not.
        // This can obviously be improved (I removed some unnecessary parts to suit this exmaple).
        string fullPath = string.Empty;

        // Make sure stored Image Location string is NOT Null or Empty.
        if (!string.IsNullOrEmpty(pictureBox.ImageLocation))
        {
            // Assign the Image Location to the local variable (to be returned).
            fullPath = pictureBox.ImageLocation;
        }

        return fullPath;
    }

写下此内容后;您就得到了逻辑,并且最终变得非常简单。

Assigning an Image to PictureBox has a certain methodology to be done correctly.
I'm sure you'll understand the reason and logic behind using the PictureBox.ImageLocation after reading this answer.

When you get an Image.FromFile; you will be required to specify a string, containing the file: Directory, FileName and Extension of the selected Image.
Therefore, once you select/load the Image to be loaded (assuming you'll be using OpenFileDialog); you have to set the file full path to the PictureBox.ImageLocation property in first place (this way you will be able to retrieve it whenever needed); and only then (preferably) you can load the Image to your PictureBox.

See example bellow:

   // Dispose PictureBox Image (Prepare it to load another Image).
    private void DisposeImage()
    {
        if (pictureBox.Image != null)
        {
            pictureBox.Image.Dispose();
            pictureBox.Image = null;
            pictureBox.ImageLocation = null;
            pictureBox.Update();
        }
    }
    

   // 1. Make sure that no Image is Loaded before Loading a new Picture.
   // 2. Set the Image Location to PictureBox.ImageLocation
   // 3. Load the Image.FromFile (Get the string from stored value).
   private void SetImage(string imageFile)
   {
       // Clear any existant Image in PictureBox.
       // [Defensive programming to prevent exceptions]
       DisposeImage();
    
       // Check if full path is valid (File Exists).
       if File.Exists(imageFile))
       {
           // Store the Image location to variable.
           pictureBox.ImageLocation = imageFile;
           
           // Load the Image from stored variable.
           pictureBox.Image = Image.FromFile(pictureBox.ImageLocation);
       }

       // Set an Image (representing "no image"); in case the Selected Image Does not exist or fails to load.
       else { pictureBox.Image = Properties.Resources.NoImageAvailable; }

       // Place your Image Configuration to Adjust to PictureBox Here.
       pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
    }
   

In case you require to get the PictureBox Image Location:

    // Retrieve Current PictureBox Image Location (if any).
    private string GetImageLocation()
    {
        // Set the default Image Location value (empty).
        // This is usefull to check wether the string is empty or not.
        // This can obviously be improved (I removed some unnecessary parts to suit this exmaple).
        string fullPath = string.Empty;

        // Make sure stored Image Location string is NOT Null or Empty.
        if (!string.IsNullOrEmpty(pictureBox.ImageLocation))
        {
            // Assign the Image Location to the local variable (to be returned).
            fullPath = pictureBox.ImageLocation;
        }

        return fullPath;
    }

After you write this down;you get the logic and it ends up in being really easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文