How to read picture from URL and show it on my page

让人想犯罪 __ 提交于 2019-12-25 00:07:51

问题


I have a sql table which holds information:

   id (hash)
   imagename string
   width int
   height int

What is the best way to create .net image read which will show images in page. I would like to call it like image.aspx/ashx?id=[id] and function will try to catch and show that image.

I know how to get data from SQL but I dont know how to read img from URL and show it as image.

Could any please point me at some relevant information how to do it or show piece of code how it works?

Do I read it as stream?

Thanks


回答1:


string imageFileName = "thefile.jpg";
context.Request.MapPath(@"IMAGES\" + context.Request.QueryString["id"]); 
context.Response.ContentType = "image/jpeg";     
context.Response.WriteFile(imageFileName);     
context.Response.Flush(); 
context.Response.Close();

http://blogs.msdn.com/b/alikl/archive/2008/05/02/asp-net-performance-sin-serving-images-dynamically-or-another-reason-to-love-fiddler.aspx

http://msdn.microsoft.com/en-us/library/ms973917.aspx




回答2:


Check out this article: http://aspnet-cookbook.info/O.Reilly-ASP.NET.Cookbook.Second.Edition/0596100647/aspnetckbk2-CHP-20-SECT-2.html

You'll want to create an HttpHandler class and wire that up in your web.config.




回答3:


You can retrieve remote resources (such as images) via HTTP using the System.Net.WebRequest class.

WebRequest request = WebRequest.Create("http://www.doesnotexist.com/ghost.png");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
BinaryReader reader = new BinaryReader(stream)
byte[] imageBytes = reader.ReadBytes(stream.Length);

Note that there might be better ways to read the bytes from the Stream. You should also remember to add using statements where appropriate to properly dispose of any unmanaged resources.



来源:https://stackoverflow.com/questions/5409884/how-to-read-picture-from-url-and-show-it-on-my-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!