2011-05-09 84 views
1

我正在创建图像比较(图像存在于目录中)基于Web的应用程序。它将特定目录中的图像与提供的图像进行比较。它会将每个图像与匹配的百分比进行比较,但不会显示来自相关目录的图像,即使我已将图像网址也提供给该图像。当我写入response.write时,它会显示与该匹配图像匹配的百分比,但不显示图像。 我写的代码为如下:在按钮单击事件中使用图像控件显示图像

 protected void btnnew_Click(object sender, EventArgs e) 
     { 

      Bitmap searchImage; 
      try 
      { 
//Image for comparing other images that are exists in directory    
searchImage = new Bitmap(@"D:\kc\ImageCompare\Images\img579.jpg"); 
      } 
      catch (ArgumentException) 
      { 
       return; 
      } 

      string dir = "D:\\kc\\ImageCompare\\Images"; 

     DirectoryInfo dir1 = new DirectoryInfo(dir); 
      FileInfo[] files = null; 
      try 
      { 
       files = dir1.GetFiles("*.jpg"); 
      } 
      catch (DirectoryNotFoundException) 
      { 
       Console.WriteLine("Bad directory specified"); 
       return; 
      } 

      double sim; 
      foreach (FileInfo f in files) 
      { 
       sim = Math.Round(GetDifferentPercentageSneller(searchImage, new Bitmap(f.FullName)), 3); 
       if (sim >= 0.95) 
       { 
        Image1.ImageUrl = dir + files[0]; 
        Image2.ImageUrl = dir + files[1]; 

        Response.Write("Perfect match with Percentage" + " " + sim + " " + f); 
        Response.Write("</br>"); 

       } 
       else 
       { 
        Response.Write("Not matched" + sim); 

       } 

      } 
     } 

回答

0

ASP.NET页面遵循基于控制发展 - 所以一般,你不会直接写入到一个回应,而更新的控制,如标签或文字文本。

就图像而言,要使图像在浏览中可见,您需要设置可从浏览器访问的网址。在上面的代码中,您正在设置物理文件路径,哪些不能作为来自相同/不同机器的URL访问。您需要将虚拟目录映射到映像存储位置,然后为此生成一个url(通过附加虚拟目录路径&文件名)或编写文件服务处理程序(ashx),该文件服务处理程序将采用映像部分路径并提供服务图像(即将其数据发送到浏览器)。