Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从URL读取图片并在我的页面上显示_C#_.net_Image_Stream_Show - Fatal编程技术网

C# 如何从URL读取图片并在我的页面上显示

C# 如何从URL读取图片并在我的页面上显示,c#,.net,image,stream,show,C#,.net,Image,Stream,Show,我有一个包含以下信息的sql表: id (hash) imagename string width int height int 创建在页面中显示图像的.net图像读取的最佳方法是什么。我想像image.aspx/ashx?id=[id]一样调用它,函数将尝试捕获并显示该图像 我知道如何从SQL中获取数据,但我不知道如何从URL中读取img并将其显示为图像 有没有人能告诉我一些相关的信息如何做,或者告诉我一段代码是如何工作的 我把它读作流吗 谢谢查看本文: 您需要创建一

我有一个包含以下信息的sql表:

   id (hash)
   imagename string
   width int
   height int
创建在页面中显示图像的.net图像读取的最佳方法是什么。我想像image.aspx/ashx?id=[id]一样调用它,函数将尝试捕获并显示该图像

我知道如何从SQL中获取数据,但我不知道如何从URL中读取img并将其显示为图像

有没有人能告诉我一些相关的信息如何做,或者告诉我一段代码是如何工作的

我把它读作流吗

谢谢

查看本文:


您需要创建一个HttpHandler类并将其连接到web.config中。

您可以使用
System.Net.WebRequest
类通过HTTP检索远程资源(如图像)

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);
请注意,可能有更好的方法来解决这个问题。还应记住在适当的情况下使用语句添加
,以正确处置任何非托管资源

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