远程下载图片

2015-12-03 15:45:33  访问(5163) 赞(0) 踩(0)


    /// <summary>
    /// 远程下载图片
    /// </summary>
    /// <param name="webSaveUrl"></param>
    /// <param name="imageUrl"></param>
    /// <returns></returns>
    protected bool DownLoadImage
        (
            string webSaveUrl,
            string imageUrl
        )
    {
        string saveName = Request.PhysicalApplicationPath + webSaveUrl;

        FileInfo theInfo = new FileInfo(saveName);

        if (theInfo.Exists)
            return false;

        if (!theInfo.Directory.Exists)
            theInfo.Directory.Create();

        WebClient my = null;
        MemoryStream ms = null;
        System.Drawing.Image img = null;
        try
        {
            // 利用 WebClient 类
            my = new WebClient();

            byte[] mybyte = null;

            mybyte = my.DownloadData(imageUrl);

            ms = new MemoryStream(mybyte);

            img = System.Drawing.Image.FromStream(ms);

            // 保存 //
            img.Save(theInfo.FullName);

            return true;
        }
        catch
        {
            return false;
        }

        finally
        {
            if (img != null)
            {

                img.Dispose();
                img = null;
            }
            if (ms != null)
            {
                ms.Dispose();
                ms = null;
            }

            if (my != null)
            {

                my.Dispose();
                my = null;
            }

        }

    }


标签:Image的操作    远程下载图片    DownLoadImage    WebClient    DownloadData 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)