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

时间:2011-08-19 06:25:27

标签: c# picturebox

当ImageLocation不起作用时,如何以Windows格式获取图片框的图片网址?

string filepath = picturebox.ImageLocation; // Returns null

7 个答案:

答案 0 :(得分:2)

如果您使用picturebox的ImageLocation属性加载图像,那么您可以获得所需的内容。其他明智的做法是,如果你通过Image属性加载它,那么你将无法从ImageLocation获得,也不会再从Image中获取它。

答案 1 :(得分:1)

你可以通过

获得
string filepath = PictureBox.ImageLocation;

请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.imagelocation.aspx

答案 2 :(得分:0)

ImageLocation

PictureBox.ImageLocation

答案 3 :(得分:0)

答案 4 :(得分:0)

听起来像你需要:

Picturebox.ImageLocation

答案 5 :(得分:0)

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

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

在需要时将其收回:

string Path = pictureBox2.Tag + string.Empty;

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

答案 6 :(得分:0)

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

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


见下例:

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

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

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

在你写下这些之后;你明白了逻辑,它最终变得非常简单。