在按钮点击事件使用Image控件显示图像 [英] Displaying image using Image control in button click event

查看:159
本文介绍了在按钮点击事件使用Image控件显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建图像比较(图像存在于目录中)基于Web应用程序。它比较图像与图像提供特定的目录存在。它的每个图像相匹配的百分比,但不显示从相关目录图像,即使我已经给出了图像的URL到图像也比较。当我写的Response.Write那么就说明与匹配的图像匹配比例,但不显示图像。
我已经写了code表示如下:

 保护无效btnnew_Click(对象发件人,EventArgs的发送)
       {           位图searchImage;
           尝试
           {
//图像比较的其他图像目录中
sea​​rchImage =新位图(@D:\\ KC \\ ImageCompare \\图片\\ img579.jpg);
           }
           赶上(ArgumentException的)
           {
               返回;
           }           字符串DIR =D:\\\\ \\\\ KC \\\\ ImageCompare图像;         DirectoryInfo的方向1 =新DirectoryInfo的(DIR);
           的FileInfo []文件= NULL;
           尝试
           {
               文件= dir1.GetFiles(*。JPG);
           }
           赶上(DirectoryNotFoundException)
           {
               Console.WriteLine(坏目录规定);
               返回;
           }           双SIM卡;
           的foreach(在文件的FileInfo F)
           {
               SIM = Math.Round(GetDifferentPercentageSneller(searchImage,新位图(f.FullName)),3);
               如果(SIM> = 0.95)
               {
                   Image1.ImageUrl = DIR +文件[0];
                   Image2.ImageUrl = DIR +文件[1];                   的Response.Write(完美的匹配百分比++ SIM ++ f)项;
                   的Response.Write(< / BR>中);               }
               其他
               {
                   的Response.Write(不匹配+ SIM卡);               }           }
       }


解决方案

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

至于那张图像,图像在浏览可见,您需要设置URL,它是从浏览器进行访问。在上述code,你设置的物理文件的路径和它不会是从同/不同计算机上的U​​RL访问。您需要使用一个虚拟目录映射到您的影像存储位置,然后生成一个相同的URL(通过附加的虚拟目录路径和放大器;文件名),或写一个文件服务处理程序(ASHX),将采取图像局部路径和服务图像(即发送其数据浏览器)。

I am creating image comparison (images exists in directory) web based application. It compares images exists in particular directory with provided image. 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).

这篇关于在按钮点击事件使用Image控件显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆