使用其URL返回图像 [英] return Image using its URL

查看:73
本文介绍了使用其URL返回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用其URL返回图像作为图像类型.
我找到了此功能,但它不返回图像.

Hi all,

I want to return image as image type using its URL.
I found this function but it does not return an image .

WebResponse response;
Stream RemoteStream;
StreamReader SR;
WebRequest request = WebRequest.Create(URL);
response = request.GetResponse();
RemoteStream = response.GetResponseStream();
SR = new StreamReader(RemoteStream);
System.Drawing.Image img = System.Drawing.Image.FromStream(RemoteStream);
img.Save(URL, System.Drawing.Imaging.ImageFormat.Jpeg);
response.Close();
RemoteStream.Close();
SR.Close();



谢谢



Thanks

推荐答案

我不确定您的代码有什么问题,但这对我有用,也许您可​​以重构它:
I''m not sure what is wrong with your code, but this works for me, perhaps you can refactor it:
WebRequest request = WebRequest.Create("http://www.dur.ac.uk/images/about/new/DurhamCastleCathedral.jpg");
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Image image = Image.FromStream(response.GetResponseStream());
image.Save(@"c:\foo.jpg", ImageFormat.Jpeg);
response.Close();



如果您不需要中间的Image对象,则应该能够直接从响应流读取到输出文件流中.这样可以节省内存,并且在某些情况下可能会更快.



You should be able to read from the response stream into the output file stream directly, if you don''t need the intermediate Image object. This will save memory, and might be quicker under certain circumstances.


这篇关于使用其URL返回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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