我有一个图片的网址,例如 "http://testsite.com/web/abc.jpg"。我想将该网址复制到我的本地文件夹"c:\images\"中; 同时,当我将该文件复制到文件夹中时,我需要将图片重命名为"c:\images\xyz.jpg"。
我们应该如何做呢?
我们应该如何做呢?
请求图片并保存,例如:
byte[] data;
using (WebClient client = new WebClient()) {
data = client.DownloadData("http://testsite.com/web/abc.jpg");
}
File.WriteAllBytes(@"c:\images\xyz.jpg", data);
你可以使用 WebClient
:
using (WebClient wc = new WebClient())
wc.DownloadFile("http://testsite.com/web/abc.jpg", @"c:\images\xyz.jpg");
这假定你实际上对 C:\images
文件夹有写入权限。
using ( WebClient webClient = new WebClient() )
{
using (Stream stream = webClient.OpenRead(imgeUri))
{
using (Bitmap bitmap = new Bitmap(stream))
{
stream.Flush();
stream.Close();
bitmap.Save(saveto);
}
}
}
string path = "~/image/";
string picture = "Your picture name with extention";
path = Path.Combine(Server.MapPath(path), picture);
using (WebClient wc = new WebClient())
{
wc.DownloadFile("http://testsite.com/web/abc.jpg", path);
}
它对我有效