首页 新闻 赞助 找找看

保存网络gif格式图片

0
悬赏园豆:15 [已解决问题] 解决于 2012-05-17 12:52

asp.net怎么保存网络的gif图片到服务器。

Emrys5的主页 Emrys5 | 菜鸟二级 | 园豆:223
提问于:2012-05-15 16:39
< >
分享
最佳答案
1
//调用示例
downRemoteImg("Images", "http://common.cnblogs.com/images/qdigg.gif");

public string downRemoteImg(string savedir, string imgpath)
        {
            if (string.IsNullOrEmpty(imgpath))
            {
                return string.Empty;
            }
            else
            {
                string imgName = string.Empty;
                string imgExt = string.Empty;
                string saveFilePath = string.Empty;
                imgName = imgpath.Substring(imgpath.LastIndexOf("/"), imgpath.Length - imgpath.LastIndexOf("/"));
                imgExt = imgpath.Substring(imgpath.LastIndexOf("."), imgpath.Length - imgpath.LastIndexOf("."));

                saveFilePath = Server.MapPath(savedir);
                if (!Directory.Exists(saveFilePath))
                    Directory.CreateDirectory(saveFilePath);
                try
                {
                    WebRequest wreq = WebRequest.Create(imgpath);
                    wreq.Timeout = 10000;
                    HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
                    Stream s = wresp.GetResponseStream();
                    System.Drawing.Image img;
                    img = System.Drawing.Image.FromStream(s);
                    switch (imgExt.ToLower())
                    {
                        case ".gif":
                            img.Save(saveFilePath + imgName, ImageFormat.Gif);
                            break;
                        case ".jpg":
                        case ".jpeg":
                            img.Save(saveFilePath + imgName, ImageFormat.Jpeg);
                            break;
                        case ".png":
                            img.Save(saveFilePath + imgName, ImageFormat.Png);
                            break;
                        case ".icon":
                            img.Save(saveFilePath + imgName, ImageFormat.Icon);
                            break;
                        case ".bmp":
                            img.Save(saveFilePath + imgName, ImageFormat.Bmp);
                            break;
                    }
                    img.Dispose();
                    s.Dispose();
                    return savedir + imgName;
                }
                catch
                {
                    return imgpath;
                }
            }
        }

Demo如上,你更改一下图片存放路径跟图片地址就可以了

收获园豆:15
artwl | 专家六级 |园豆:16736 | 2012-05-15 16:59

这样保存的gif只有一帧,我想保存完整的gif

Emrys5 | 园豆:223 (菜鸟二级) | 2012-05-15 17:03

@ninglee515: 改成这样:

using (WebClient wc = new WebClient())
{
      wc.DownloadFile("http://space.cnblogs.com/Emoticons/others/drink.gif", Server.MapPath("Images") + "\\test.gif");
}
artwl | 园豆:16736 (专家六级) | 2012-05-15 17:09

@artwl: 恩谢谢。还想问个问题啊。就是我保存网络图片是直接用webclient呢还是除了gif用webclient其他格式的用System.Drawing.Image。他们的效率怎么样!

Emrys5 | 园豆:223 (菜鸟二级) | 2012-05-16 08:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册