我从数据库中读取出了二进制流,现在遇到的困难是如何把该二进制流转换成图片,注意是在.aspx网页中显示图片不是在桌面应用程序中显示图片

解决方案 »

  1.   

            //打开文件图片,并存放在文件流中
            FileStream stream = new FileStream(Server.MapPath("logo.jpg"),FileMode.Open); //首先声明一个文件流的类,并且初始化,使用Server.MapPath方法获取到绝对路径,使用FileMode.Open设置图片文件模式为打开。
            long filesize = stream.Length;//声明一个长整形变量,用来保存文件流的长度。
            byte[] buffer=new byte[(int)filesize];//定义一个二进制的数组,数组大小就是文件流的长度
            stream.Read(buffer, 0, (int)filesize);//用文件流的read方法来读取这个文件,并将该数据写入缓冲区内。
            stream.Close();//关闭流
            Response.BinaryWrite(buffer);//将图片输出到页面中
      

  2.   

    你要先将图片读到一个文件夹内,然后把路径给它就可以了,参考:
    MemoryStream ms=new MemoryStream ((Byte[])imgname);//imgname是图片的名字
    Bitmap img=new Bitmap(ms);
    string filepath=Server.MapPath("Files/");
    DirectoryInfo dir=new DirectoryInfo(filepath);
    FileInfo[] filecount=dir.GetFiles();
    int i=filecount.Length;
    string imagename=filepath+(i+1)+.jpg);
    img.Save(imagename);Image1.ImageUrl="Files/+((i+1)+".jpg");
      

  3.   

            SqlConnection conn = new SqlConnection(".........");
            SqlCommand comm = new SqlCommand("select image字段 from 表 where id=1", conn);
            conn.Open();
            SqlDataReader reader = comm.ExecuteReader();
            if (reader.Read())
            {
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite((Byte[])reader["image字段"]);
            }
            Response.End();
            conn.Close();
      

  4.   

     
    int Id=int.Parse(Request.QueryString["id"]);  
    using(SqlConnection conn=new SqlConnection())  
    {  
    conn.ConnectionString="";  
        
    string strSql="select * from Tb where Id='"+Id+"'";  
    SqlCommand cmd=new SqlCommand(strSql,conn) ;  
    conn.Open();  
    SqlDataReader reader=cmd.ExecuteReader();  
        
    if(reader.Read())  
    {  
    Response.ContentType = "image/jpeg";  
    Response.BinaryWrite((Byte[])reader["Photo"]);  
    }  
    Response.End();  
    conn.Close();  
    }