开发者

Displaying image using Image control in button click event

开发者 https://www.devze.com 2023-03-03 16:34 出处:网络
I am creating image comparison (images exists in directory) web based application. It compares images exists in particular directory with provided i开发者_运维技巧mage. It compares each image with mat

I am creating image comparison (images exists in directory) web based application. It compares images exists in particular directory with provided i开发者_运维技巧mage. It compares each image with matching percentage but not displaying images from related directory even i have given the image url to that images also. When i write response.write then it shows matching percentage with that matching image, but not displaying images. I have written code for that as follows :

     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);

               }

           }
       }


ASP.NET pages follows control based development - so generally, you don't directly write to a response but rather update text for control such as label or literal.

As far as image goes, for image to visible in the browse, you need to set the url that is accessible from the browser. In above code, you are setting the physical file path and which is not going to be accessible as a URL from same/different machine. You need either to map a virtual directory to your image storage location and then generate a url for the same (by appending virtual directory path & file name) or to write a file serving handler (ashx) that would take the image partial path and serve the image (i.e. send its data to browser).

0

精彩评论

暂无评论...
验证码 换一张
取 消