疯了,2个小时还没有搞定一个图片显示,
C/S, winform中。
代码如下:
Bitmap bm ;

byte[] bytes ;
  
SqlConnection con = new SqlConnection("server = 。; database =。; uid  = 。。; password =。。;"); 
string sql  = " select * from picteset where id = '"+num+"' ";
              
SqlCommand cmd = new SqlCommand(sql , con );SqlDataReader dr ;
cmd.Connection.Open();
dr= cmd.ExecuteReader();
if(dr.HasRows)
{   dr.Read();
    bytes = (byte[])dr["pic"];
    System.IO.MemoryStream ms = new MemoryStream(bytes, 0 , bytes.Length);
    ms.Position=0;
    Image im = Image.FromStream(ms,true);
    this.pictureBox2.Image = im; // Stream s = new Stream();                
// s.Write(bytes ,0,bytes.Length);
// bm = new Bitmap(s);

}开始的时候,我打算用Stream 保存bytes .
结果,说 “无法建立抽象类别或界面'System.IO.Stream'的实体”后来上网看,看到很多大哥,用MemoryStream 来保存bytes. 代码如上(为注释部分)
可以又提示 ,引用了无效的参数。
我看了一下, image.FromStream()中只有Stream类的参数 ,没有MemoryStream的参数
所以有错误。同样的,bitmap对象,也没有引用MemoryStream 对象作参数的。重点, MemoryStream 不能转化为 Stream 对象上网看了好多帖子,没有找到正届,求 : 可以 实现问题中功能的可执行代码 ? 得到代码立刻结帖

解决方案 »

  1.   

    用MemoryStream没有任何问题,
    存的时候如下:
    MemoryStream ms = new MemoryStream();
    yourBitmap.Save( ms, ImageFormat.Jpeg );
    ms.Flush();
    byte[] bData = ms.GetBuffer();
    ms.Close();
    //Save data into db using "bData"取得时候如下:
    MemoryStream ms = new MemoryStream( yourBytes );
    Bitmap bit = new Bitmap( ms );
      

  2.   

    这个两年前做过,现在只有一点印象了,就是用MemoryStream来实现的,晚上回去以后给我贴代码出来吧!
      

  3.   

    真的不可以,
    在说明一点,我用的。NET 1.1 ,
    不是 2.0 ,
    不知道,这个会不会有影响 。
    早上,调试 FTP 就没有成功,后来才发现,人家使用的是FTP2.0的新类,求高手指点
      

  4.   

    //
    Double-click Button2, and then add the following code to the Button2_Click event handler.Note Uid <user name> must have permissions to perform these operations on the database.{
    SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");byte[] MyData= new byte[0];
       
    da.Fill(ds, "MyImages");
    DataRow myRow;
    myRow=ds.Tables["MyImages"].Rows[0];
               
    MyData =  (byte[])myRow["imgField"];
    int ArraySize = new int();
    ArraySize = MyData.GetUpperBound(0); FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(MyData, 0,ArraySize);
    fs.Close();
    }
      

  5.   

    你把流直接给image的url就可以
      

  6.   

    恩,
    可是我要在winform上面实现
      

  7.   

    http://www.51one.net/study/aspnet/11075.html
      

  8.   

    刚测试通过的代码
    con.Open();
    dr = com.ExecuteReader();
    if(dr.Read())
    {
    byte[] image =(byte[]) dr[0];
    MemoryStream  ms = new MemoryStream(image);
    this.pictureBox1.Image = Image.FromStream(ms);

    }
      

  9.   

    SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=pubs");
    SqlCommand cmd = new SqlCommand("Select pub_id,logo FROM pub_info", cn);FileStream fs;
    BinaryWriter bw;//缓冲区大小
    const int bufferSize = 100;
    byte [] outByte = new byte[bufferSize];
    //GetBytes返回的字节数量
    long retval;
    //BLOB输出的起始位置
    long startIndex = 0;string pub_id = "";cn.Open();SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);while(dr.Read())
    {
    pub_id = dr.GetString(0);fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
    bw = new BinaryWriter(fs);startIndex = 0;do
    {
    retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
    //Console.WriteLine(retval.ToString());
    bw.Write(outByte);
    bw.Flush();
    startIndex += bufferSize;
    }while(retval == bufferSize);bw.Write(outByte, 0, (int)retval - 1);
    bw.Flush();bw.Close();
    fs.Close();
    }dr.Close();
    cn.Close();
    }