获取Picturebox文件名 [英] Get Picturebox file name
本文介绍了获取Picturebox文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class photo
picture pbx = new picturebox();
private voide btnok( object sender,EventArgs e)
{
string [] files = System.IO.Directory.GetFiles( @ d:\Building_Case跨度>);
for each( string file in files)
{
pbx.image = image.fromfile(file);
pbx.imagelocation = file;
}
pbx.DoubleClick + = pbx_DoubleClick;
}
void pbx_Double_Click( object sender,EventArgs e)
{
string path = pbx.imagelocation;
messagebox.show(path);
}
现在,路径返回最后一个索引。我想在双击每个图片框时显示每个路径名称。所以,PLZ帮助我
添加了代码块,索引改进了,最后删除了额外的}[/ edit]
解决方案
只有在调用Load方法时,PictureBox.ImageLocation才会相关: http://msdn.microsoft.com/en-us/library/system.windows .forms.picturebox.imagelocation(v = vs.110).aspx [ ^ ] - 在设置图像属性时从不使用它,因为图像可能根本没有路径,即使它确实如此,Image控件可能也不会意识到它(有很多种方法可以将图像转换为Image类实例,而且很少有直接转换文件的方法)。
相反,改变你的装载公司de:
每个( string 文件在文件中)
{
pbx.image = image.fromfile(file);
pbx.Tag = file;
}并在点击方法中使用Tag属性:
string path = pbx.Tag as string ;
if (!String.IsNullOrWhiteSpace(path))
{
MessageBox.Show(path);
}
foreach ( string file in files)
{
pbx.image = image.fromfile (文件);
FileInfo info = new FileInfo(file);
// 从信息对象获取文件信息
string FileName = info.Name;
}
您好b $ b尝试此代码..
添加评论,请检查。
private void button1_Click( object sender,EventArgs e)
{
string [] files = System.IO.Directory.GetFiles( @ D:\Building_Case 跨度>);
foreach ( var 文件 in 文件)
{
pbx.Image = Image.FromFile(file);
pbx.ImageLocation = file; // 将路径存储在此属性中
}
pbx.DoubleClick + = new EventHandler(pbx_DoubleClick);
}
void pbx_DoubleClick( object sender,EventArgs e)
{
string path =(sender as PictureBox)。 ImageLocation; // 从此属性获取路径
MessageBox.Show(path);
System.Diagnostics.Process.Start(path); // 在Windows Image Viewer中打开图像
}
public class photo
picture pbx =new picturebox();
private voide btnok(object sender, EventArgs e)
{
string[] files=System.IO.Directory.GetFiles(@"D:\Building_Case");
for each(string file in files)
{
pbx.image=image.fromfile(file);
pbx.imagelocation=file;
}
pbx.DoubleClick+=pbx_DoubleClick;
}
void pbx_Double_Click(object sender, EventArgs e)
{
string path=pbx.imagelocation;
messagebox.show(path);
}
Now, path returns last index. I want to show each path name when I double click on each picturebox. So, plz help me
[edit]Code block added, indexation improved and extra "}" at the end deleted[/edit]
解决方案
The PictureBox.ImageLocation is only ever relevant when you have called the Load method: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.imagelocation(v=vs.110).aspx[^] - it is never used when you set the Image poperty, because the Image may not have a path at all, and even if it does, the Image control may not be aware of it (there are a huge number of ways to get an image into an Image class instace, and very few of them directly inmvolve files).
Instead, change your loading code:
for each(string file in files) { pbx.image=image.fromfile(file); pbx.Tag = file; }And use the Tag property in your click method:
string path=pbx.Tag as string; if (!String.IsNullOrWhiteSpace(path)) { MessageBox.Show(path); }
foreach(string file in files) { pbx.image=image.fromfile(file); FileInfo info = new FileInfo(file); //Get file infromation from info object string FileName = info.Name; }
Hi Try this code..
added comments , pls check it.
private void button1_Click(object sender, EventArgs e) { string[] files = System.IO.Directory.GetFiles(@"D:\Building_Case"); foreach (var file in files) { pbx.Image = Image.FromFile(file); pbx.ImageLocation = file; // store the path also in this Property } pbx.DoubleClick += new EventHandler(pbx_DoubleClick); } void pbx_DoubleClick(object sender, EventArgs e) { string path = (sender as PictureBox).ImageLocation; // get the path from this property MessageBox.Show(path); System.Diagnostics.Process.Start(path); // To open the image in Windows Image Viewer }
这篇关于获取Picturebox文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文