[英]Image Preview asp.net with c#
我正在得到一个image
到一个picturebox
上从asp.net list-box
,该list-box
读取目录,然后填充jpegs
的文件中。 这需要在c#中完成,目前我对它的完成方式有一个大概的了解,但是我没有得到任何显示的图片:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DirectoryInfo infoDir = new DirectoryInfo(@"G:/Test_Directory");
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name);
}
}
}
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
Server.MapPath(lstDirectory.SelectedValue.ToString());
imageChange.ImageUrl = lstDirectory.SelectedValue.ToString();
}
}
可能是路径不正确的情况,或其他原因。 有人可以将我定向到哪里出问题了。
您没有使用MapPath
的结果。 尝试这个。
var img = Server.MapPath(lstDirectory.SelectedValue.ToString());
imageChange.ImageUrl = img;
更新:您的图像文件文件夹似乎在web文件夹之外,将其移入其中。 没有其他简单的方法可以使它正常工作。
我建议您在解决方案中add New folder
, add all your images in to it...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DirectoryInfo infoDir = new DirectoryInfo(Server.MapPath("Images"));//this is the image foled name
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name,"Images/"+file.Name);
//here you are setting relative path of images in your value field
}
}
}
然后将图像路径设置为thr Server.MapPath,如下所示。
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
if(File.Exists(Server.MapPath(lstDirectory.SelectedValue.ToString())))
{
imageChange.ImageUrl =Server.MapPath(lstDirectory.SelectedValue.ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.