
只需 使用以下方法即可。
using (WebClient client = new WebClient()) { client.DownloadFile(new Uri(url), @"c:tempimage35.png"); // OR client.DownloadFileAsync(new Uri(url), @"c:tempimage35.png");}这些方法与DownloadString(..)和DownloadStringAsync(…)几乎相同。他们将文件存储在目录中,而不是C#字符串中,并且不需要URi中的格式扩展名
如果您不知道图像的格式(.png,.jpeg等)public void SaveImage(string filename, ImageFormat format){ WebClient client = new WebClient(); Stream stream = client.OpenRead(imageUrl); Bitmap bitmap; bitmap = new Bitmap(stream); if (bitmap != null) { bitmap.Save(filename, format); } stream.Flush(); stream.Close(); client.Dispose();}使用它try{ SaveImage("--- Any Image Path ---", ImageFormat.Png)}catch(ExternalException){ // Something is wrong with Format -- Maybe required Format is not // applicable here}catch(ArgumentNullException){ // Something wrong with Stream}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)