How to get the image URL of a picture box in windows form when ImageLocation doesn't work?
string filepath = picturebox.ImageLocation; // Returns nul开发者_C百科l
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.
you can get that via
string filepath = PictureBox.ImageLocation;
see http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.imagelocation.aspx
ImageLocation
PictureBox.ImageLocation
May be this post can help you http://blogs.msdn.com/b/marcelolr/archive/2007/06/15/data-binding-a-winforms-picturebox-to-a-file-path-or-url.aspx
cheers
Sounds like you need:
Picturebox.ImageLocation
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.
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.
精彩评论