简体   繁体   English

图像预览asp.net与C#

[英]Image Preview asp.net with c#

I'm working on getting an image into a picturebox on asp.net from a list-box , the list-box reads a directory and then populates the jpegs in the file. 我正在得到一个image到一个picturebox上从asp.net list-box ,该list-box读取目录,然后填充jpegs的文件中。 This needs to be done in c#, at the moment I have a rough idea on how it's done but I'm not getting any picture showing: 这需要在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();            
    }
}

it could be a case of the path not being correct, or maybe something else. 可能是路径不正确的情况,或其他原因。 Can someone could direct me to where I'm going wrong. 有人可以将我定向到哪里出问题了。

You are not using the result of MapPath . 您没有使用MapPath的结果。 Try this. 尝试这个。

    var img = Server.MapPath(lstDirectory.SelectedValue.ToString());
    imageChange.ImageUrl = img;  

UPDATE: Your image files folder seems to be outside of web folder, move it inside. 更新:您的图像文件文件夹似乎在web文件夹之外,将其移入其中。 There is no simple way to make it work otherwise. 没有其他简单的方法可以使它正常工作。

I would suggest you to add New folder in your solution and add all your images in to it... 我建议您在解决方案中add New folderadd 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
          }
        }    
    }

Then set the image path to thr Server.MapPath as follow.... 然后将图像路径设置为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM