如何在表单上显示图像 [英] how to show image on the form

查看:85
本文介绍了如何在表单上显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单来保存员工信息,包括员工形象,使用以下代码正确保存图像。

 Employee.ImgBody =(Employee.ImgBody == < span class =code-keyword> null )?  new   byte  [ 0 ]:员工。 ImgBody; 
// 图像保存代码
字节 [] imgByte = null ;
if (txtEpPicture.HasFile&& txtEpPicture.PostedFile!= null
{
// 创建PostedFile
HttpPostedFile文件= txtEpPicture。 PostedFile;
// 使用文件len创建字节数组
imgByte = new Byte [File.ContentLength];
// 强制控件在数组中加载数据
File.InputStream。读取(imgByte, 0 ,File.ContentLength);
Employee.ImgBody = imgByte;
}



现在我想在用户选择任何员工时显示该图像,并且如果用户需要也想更新该图像。我想通过使用以下代码将图像显示到表单中

 empPicture.ImageUrl =  string  .Format(   EmpPicture.ashx?id = {0},Employee.Id); 



i有搜索并尝试谷歌的不同解决方案,但都是徒劳的,图像是以varbinary的形式在db中。

解决方案

< blockquote>这个问题根本与Web表单无关。它仅与用于上传的HTML有关:您可以使用< input type =file/> ;它必须是某种Web形式,带有Sumbit按钮( type =submit;有关基本信息,请参阅:http://www.w3schools.com/html/html_forms.asp )。



这是一个最小的解决方案,同时也是最合理的解决方案之一:

 <   img     src   =  myUploadedImage.png  /  >  



现在,按照这个简单的逻辑:所有计数,你已经有服务器端处理和处理上传。如果你想避免在服务器端文件系统中保存文件,它可能会有点复杂,但你不想要它,你想要保存文件。如果你想在演示之前对文件进行一些后处理,那可能会有点复杂,但显然你没有做那样的事情。但是,您不需要从HTTP请求中读取任何内容并进行任何处理,除非在特定文件名下保存文件并在< img> 元素中引用此文件名您的HTTP响应中生成的HTML,无论您如何操作。



有很多关于处理上传的文章。让我们考虑一个ASP.NET示例:您可以使用类 System.Web.UI.WebControls.FileUpload

https://msdn.microsoft.com/en-us/ library / system.web.ui.webcontrols.fileupload%28v = vs.110%29.aspx

,例如,http://asp.net-tutorials.com/controls/file-upload-control



请注意上面代码示例参考中的 SaveAs 调用。这就是你所需要的,没有流读,没有其他处理;一切都已经由ASP.NET完成了。

如果你使用别的东西,这个想法将完全一样。



-SA

i have a form to save employee information including employee image,image is saving properly using following code.

Employee.ImgBody = (Employee.ImgBody == null) ? new byte[0] : Employee.ImgBody;
               //Image saving code
               Byte[] imgByte = null;
               if (txtEpPicture.HasFile && txtEpPicture.PostedFile != null)
               {
                   //To create a PostedFile
                   HttpPostedFile File = txtEpPicture.PostedFile;
                   //Create byte Array with file len
                   imgByte = new Byte[File.ContentLength];
                   //force the control to load data in array
                   File.InputStream.Read(imgByte, 0, File.ContentLength);
                   Employee.ImgBody = imgByte;
               }


now i want to show that image when user select any employee and also want to update that image if user need. i want to show image into the form by using following code

empPicture.ImageUrl = string.Format("EmpPicture.ashx?id={0}", Employee.Id);


i have search and try different solution from the google,but all in vain,image is in db in the form of varbinary.

解决方案

This "issue" is not related to Web forms at all. It is only related to the HTML use for uploading: you can use <input type="file"/>; and it has to be in some Web form, with "Sumbit" button (type="submit"; for basic information, please see: http://www.w3schools.com/html/html_forms.asp).

This is a minimal solution, one one of the most practically reasonable at the same time:

<img src="myUploadedImage.png"/>


Now, follow this simple logic: by all counts, you already have server-side processing and handle uploads. If you wanted to avoid saving a file in your server-side file system, it could be a bit more complex, but you don't want it, you want to save the file. If you wanted to do some post-processing of file before presentation, it could be a bit more complex, but apparently you are not doing anything like that. But then you don't need to read anything from HTTP request and do any processing except saving the file under certain file name and referencing this file name in the <img> element of your HTML generated in your HTTP response, no matter how you do it.

There are a lot of articles written on handling uploads. Let's consider one ASP.NET example: you can use the class System.Web.UI.WebControls.FileUpload:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload%28v=vs.110%29.aspx,
and, for example, http://asp.net-tutorials.com/controls/file-upload-control.

Pay attention form the SaveAs call in the code sample references above. This is all you need, no stream reading, no other processing; everything is already done by ASP.NET.
If you use something else, the idea will be exactly the same.

—SA


这篇关于如何在表单上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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