C# 如何以编程方式保存来自 URL 的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1110246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I programmatically save an image from a URL?
提问by
How do I programmatically save an image from a URL? I am using C# and need to be able grab images from a URL and store them locally. ...and no, I am not stealing :)
如何以编程方式保存来自 URL 的图像?我正在使用 C# 并且需要能够从 URL 抓取图像并将它们存储在本地。...不,我不是在偷:)
采纳答案by Alex
It would be easier to write something like this:
写这样的东西会更容易:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
回答by William Edmondson
You just need to make a basic http request using HttpWebRequestfor the URI of the image then grab the resulting byte stream then save that stream to a file.
您只需要使用HttpWebRequest为图像的 URI发出基本的 http 请求,然后获取生成的字节流,然后将该流保存到文件中。
Here is an example on how to do this...
这是有关如何执行此操作的示例...
'As a side note if the image is very large you may want to break up br.ReadBytes(500000)into a loop and grab n bytes at a time writing each batch of bytes as you retrieve them.'
'作为旁注,如果图像非常大,您可能希望将br.ReadBytes(500000)分解成一个循环,并在检索它们时一次写入每批字节的 n 个字节。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace ImageDownloader
{
class Program
{
static void Main(string[] args)
{
string imageUrl = @"http://www.somedomain.com/image.jpg";
string saveLocation = @"C:\someImage.jpg";
byte[] imageBytes;
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();
Stream responseStream = imageResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream ))
{
imageBytes = br.ReadBytes(500000);
br.Close();
}
responseStream.Close();
imageResponse.Close();
FileStream fs = new FileStream(saveLocation, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(imageBytes);
}
finally
{
fs.Close();
bw.Close();
}
}
}
}
回答by opsgreat
An example in aspx (c#)
aspx (c#) 中的一个例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
public partial class download_file_from_url : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://4rapiddev.com/wp-includes/images/logo.jpg";
string file_name = Server.MapPath(".") + "\logo.jpg";
save_file_from_url(file_name, url);
Response.Write("The file has been saved at: " + file_name);
}
public void save_file_from_url(string file_name, string url)
{
byte[] content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
content = br.ReadBytes(500000);
br.Close();
}
response.Close();
FileStream fs = new FileStream(file_name, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(content);
}
finally
{
fs.Close();
bw.Close();
}
}
}
Author: HOAN HUYNH
ASP.Net C# Download Or Save Image File From URL
作者:HOAN HUYNH
ASP.Net C# 从 URL 下载或保存图像文件
回答by Paolo Softlogica
My solution is pre save the image to de disk and then usa as a normal saved image:
我的解决方案是将图像预先保存到磁盘,然后作为普通保存的图像使用:
remoteFile = "http://xxx.yyy.com/image1.png"; localFile = "c:\myimage.png";
remoteFile = " http://xxx.yyy.com/image1.png"; localFile = "c:\myimage.png";
WebClient webClient = new WebClient(); webClient.DownloadFile(remoteFile, localFile);
WebClient webClient = new WebClient(); webClient.DownloadFile(remoteFile, localFile);