日期:2014-05-18  浏览次数:21065 次

已知一个网上图片地址(如http://sina/image.jpg)怎样通过.net把此图片变成自己的图片保存下来
已知一个网上图片地址(如http://sina/image.jpg)怎样通过.net把此图片变成自己的图片保存下来
存放在已有的文件夹里..个位高手请指点.求源码

------解决方案--------------------
http://www.gdwg.net/article/sort014/sort063/sort071/info-1576.html
------解决方案--------------------
string s = txtURL.Text.Trim();
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(s);
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
Image myImage = (Bitmap)Image.FromStream(res.GetResponseStream());

......
然后想怎样就怎样了
------解决方案--------------------
C# code

 private  Stream GetStream(string strLink)
    {
        HttpWebRequest request = null;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(strLink);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)";
            return request.GetResponse().GetResponseStream();
        }
        catch
        {
            request.Abort();
            return null;
        }
    }

    public bool SaveImage(string strLink,string strFolder,string strNewName,bool unique)
    {
        try
        {
            string strTmp = string.Empty;
            readstream = GetStream(strLink);
            if (readstream == null)
            {
                throw new Exception("获取图片错误");
            }
      
            if (unique)
            {
                filestream = new FileStream(GetPath(strLink,strFolder,strNewName), FileMode.CreateNew, FileAccess.Write);
            }
            else
            {
                filestream = new FileStream(GetPath(strLink, strFolder, strNewName), FileMode.Create, FileAccess.Write);
            }
            do
            {
                length = readstream.Read(buffer, 0, buffer.Length);
                if (length == 0)
                {
                    break;
                }
                filestream.Write(buffer, 0, length);
            }
            while (length > 0);
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            Close();
        }
    }

//流都是stream类型

我写ftp的时候用到的,,,具体的你自己改!
close这个函数的命令就是关闭这些流!!