如何用C#将服务器上的图像文件下载到本地上?现在已经知道图像的路径了(如192.168.1.102的服务器上的D:/ct/),我试着写了一些,但下面不知该怎么写了,请各位大虾指导一下小弟:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace test_dicom
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {           
            System.Net.HttpWebRequest reg =(System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://192.168.1.102/Inetpub/wwwroot/UChat/WebForms/图像/ct/1");//这里报错,服务器的相关文件共享了呀,Create参数的格式不对?
            System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)reg.GetResponse();
            System.IO.Stream s = new System.IO.MemoryStream();
            s = resp.GetResponseStream();
            Byte[] myArray=new Byte[s.Length];
            s.Read(myArray,0,(int)s.Length);
            //下面?
        }
    }
}

解决方案 »

  1.   

    你这个路径写得很奇怪...你先用IE访问看看能看到图片吗?这样更像一些
    http://192.168.1.102/UChat/WebForms/图像/ct/1

    http://localhost/UChat/WebForms/图像/ct/1
      

  2.   

      谢谢zswang大哥!!!!我的路径确实不对,因为IE访问不到,但改成您说的“http://192.168.1.102/UChat/WebForms/图像/ct/1”就可以了,但执行到Byte[] myArray=new Byte[s.Length]; 时就又提示错误,好像说不支持检索。
      也就是说现在http响应流过来了,但我不知道怎么把它存到本地,具体语句怎么实现呢?试了几种方法老是不行,哪位好心的大哥能帮俺写一下吗?感激涕零
      

  3.   

    在PictureBox中有实现代码,你下Reflector就可以看到。
    PictureBox.Load();
      

  4.   

    用webClient类,WebClient类是对HttpWebRequest和HttpWebResponse封装后的类,使用更简单。
    事例:
      System.Net.WebClient wc=new System.Net.WebClient();  wc.DownloadFile("http://www.baidu.com/logo.gif","C:\\logo.gif");
      

  5.   

    服务器上的图像文件不是常用的图像格式(bmp、jpeg、gif等),而是医学图像格式DICOM,不能直接在PictureBox中显示,所以我想先下到本地,保存成图像文件,再进行处理显示,但保存这一步还没做到呢,汗~~,我是新手,还望各位大哥帮帮小弟,感激涕零!!!!!!
      

  6.   

    另外,wo798大哥,我的是移动项目,用的.NET Compact Framework,它不支持webClient类呀,只能用HttpWebRequest和HttpWebResponse呀
      

  7.   

    LZ试试这样:
    private Image GetImage(string path)
    {
        WebRequest state = WebRequest.Create(this.CalculateUri(path));    try
        {
            WebResponse response = state.GetResponse();
            int contentLength = (int)response.ContentLength;
            Stream responseStream = response.GetResponseStream();
            return Image.FromStream(responseStream);
        }
        catch
        {
            // Error.
        }
        return null;
    }private Uri CalculateUri(string path)
    {
        try
        {
            return new Uri(path);
        }
        catch (UriFormatException)
        {
            path = Path.GetFullPath(path);
            return new Uri(path);
        }
    }
      

  8.   

    关键不在哪种方式,而在你的路径是文件夹而不是文件.所以如果这个文件夹所在的虚拟目录设置了"浏览"权限的话,你会得到文件列表的html输出(而不是你要的图片). 如果这个目录没有设置"浏览"权限,则你会得到一个错误说不支持检索.
    只有一种情况你可以这么写,就是iis里面设置了这个图片文件为"默认文档"之一. 一般来说这是不可能的.你只要把路径后面加个文件名就OK了.
      

  9.   

    但我已经成功下载到手机上了,从网上查的,代码如下:
           private void button1_Click(object sender, EventArgs e) 
            {            
                System.Net.HttpWebRequest reg =(System.Net.HttpWebRequest);
                 System.Net.WebRequest.Create("http://192.168.1.102/UChat/WebForms/图像/ct/1"); 
                 System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)reg.GetResponse(); 
                Stream inStream = null;
                FileStream fileStream = null;
                try
                {
                  inStream = resp.GetResponseStream();
                  long fileSizeInBytes = response.ContentLength;
                 fileStream = new FileStream(“My Douments\\ct1.dcm”, FileMode.OpenOrCreate,
                                           FileAccess.Write);
                 //读取数据缓冲区长度和缓冲区
                   int length = 1024;
                  byte[] buffer = new byte[1025];
                 //记录读取的长度
                   int bytesread = 0;
                  //从网络读取数据
                   while((bytesread = inStream.Read(buffer, 0, length)) > 0)
                  {
                     //把数据写入文件
                        fileStream.Write(buffer, 0, bytesread);
                  }
                  MessageBox.Show("写入结束");
                }
               catch(Exception fe)
               {
                MessageBox.Show("文件创建/写入错误:"+fe.Message,"提示信
                      息",MessageBoxButtons.OK,MessageBoxIcon.Information);            }
               finally
               {
                  if(inStream != null)
                  {
                    inStream.Close();
                  }
                 if(fileStream != null)
                  {
                    fileStream.Close();
                  }
                }      }
    虽然成功了,但总感觉缓冲区设的是固定的,一旦图像大小超过1024,会不会就出错呢?各位大侠,提点意见,优化以下。
       另外,我把图像保存到本地了,但显示或处理时还要把它读出来,进行解析或转换,这样会不会影响速度呢?是不是放在缓存中直接进行解析转换速度会快一些?但又担心图像大了,缓存放不下(因为智能手机的内存不能和PC比呀),挺矛盾的。大家有什么看法呢?
      

  10.   

    你直接用Image.FromStream就可以了,不用存到文件缓冲,Image自己会处理。
      

  11.   

    水月流影大哥:
      你说的是把处理完的图像像素数组转换成流,用Image.FromStream方法赋给PictureBox.Image属性吗?能不能说的具体一点呢?小弟不太明白。谢谢
      

  12.   

    你可以这样:// PictureBox pb = new PictureBox();
    // inStream = resp.GetResponseStream(); 
    pb.Image = Image.FromStream(inStream).Clone() as Image;以下是Image.FromStream在MSDN上的解释:从指定的数据流创建 Image。 命名空间:System.Drawing
    程序集:System.Drawing(在 system.drawing.dll 中)语法public static Image FromStream (
    Stream stream
    )
    参数
    stream
    Stream,包含该 Image 的数据。 返回值
    此方法创建的 Image。 
    异常
    异常类型 条件 
    ArgumentException
     该流没有有效的图像格式- 或 -stream 为空引用(在 Visual Basic 中为 Nothing)。
     备注
    在 Image 的生存期内,必须使流保持打开。如果用相同的流连续调用该方法,该流将重置为零。平台
    Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。版本信息
    .NET Framework
    受以下版本支持:2.0、1.1、1.0
      

  13.   

    但我的图像流是医学图像格式DICOM,Image类不支持这种格式吧,好像我要转换成普通图像格式才行吧