我做了一个把图片转换成二进制存入oracle数据库的程序,我想把它从数据库里读出来并显示出来,希望是在后台写代码,请高手们帮帮忙!

解决方案 »

  1.   

    Image.Save(
       Stream stream,
       ImageFormat format
    );
      

  2.   

    TypeConverter.ConvertFrom 
    public object ConvertFrom(
       object value
    );
    将给定值转换为此转换器的类型。
      

  3.   

    hdt(倦怠) 你能说清楚点吗,我是新手,谢谢
      

  4.   

    void savejpg(String NAME)
    {
        SqlConnection CON = new SqlConnection();
        SqlCommand CMD = new SqlCommand("SQL");    FileStream fs;
        BinaryWriter bw;    int bufferSize = 1024;
        byte[] outbyte = new byte[bufferSize];
        long retval;
        long startIndex = 0;    CON.Open();
        SqlDataReader myReader = CMD.ExecuteReader(CommandBehavior.SequentialAccess);    while (myReader.Read())
        {
            fs = new FileStream(NAME, FileMode.OpenOrCreate, FileAccess.Write);
            bw = new BinaryWriter(fs);        startIndex = 0;
            retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);        while (retval > 0)
            {
                bw.Write(outbyte);
                bw.Flush();
                startIndex += bufferSize;
                retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
            }        bw.Write(outbyte);
            bw.Flush();        bw.Close();
            fs.Close();
        }    myReader.Close();
        CON.Close(); }
    类似
      

  5.   

    从数据读图片代码比较长
    就不写了byte[] barrImage = 数据库存图片的二进制字段值
    MemoryStream ms = new MemoryStream( barrImage );
    Image image = Image.FormStream(ms);
    image.Save( Response.OutputStream , ImageFormat.Jpeg )
      

  6.   

    Image 的引用为什么总是报错,不是using System.Drawing.Imaging;吗
      

  7.   


    命名空间: System.Drawing
      

  8.   

    为什么还是报Image是不明确的引用
      

  9.   

    c:\inetpub\wwwroot\WebApporacle\WebForm1.aspx.cs(51): 找不到类型或命名空间名称“image”(是否缺少 using 指令或程序集引用?)
      

  10.   

    c:\inetpub\wwwroot\WebApporacle\WebForm1.aspx.cs(50): “Image”是不明确的引用
      

  11.   

    考虑是否你的程序里有叫Image的类
    另用System.Drawing.Image 代替Image
      

  12.   

    用System.Drawing.Image 代替Image应该不会再报不明确的引用
      

  13.   

    private Image RenderImage(byte[] byteImg)
    {
    System.Drawing.Image image1 = System.Drawing.Image.FromStream(new MemoryStream(byteImg));
    return image1;
    }
      

  14.   

    System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
    使用了无效参数。 
      

  15.   

    大家帮我 看看代码吗
    public string bind()
    {
    StringBuilder sb=new StringBuilder();
    strsql="select docid,fileid,rowed,filecon from docmapfile";
    OracleDataAdapter da = new OracleDataAdapter(strsql,conn);
    da.Fill(ds);

    this.text1.Value=ds.Tables[0].Rows[0].ItemArray[0].ToString();
    this.text2.Value=ds.Tables[0].Rows[0].ItemArray[1].ToString();
    this.text3.Value=ds.Tables[0].Rows[0].ItemArray[2].ToString(); byte[] barrImage=(byte[])ds.Tables[0].Rows[0].ItemArray[3];
    MemoryStream ms = new MemoryStream(barrImage);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
    image.Save( Response.OutputStream , ImageFormat.Jpeg );
    sb.Append(image.ToString()); conn.Close();

    return sb.ToString();
    }
      

  16.   

    MemoryStream ms = new MemoryStream( barrImage );
    Image image = Image.FormStream(ms,true);
    image.Save( Response.OutputStream ,ImageFormat.Jpeg )
    关键就是这里
      

  17.   

    我就是System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
    这里报错,使用了无效参数。
      

  18.   

    to 我就是System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
    这里报错,使用了无效参数。我刚遇到了个类似的错误,请检查一下你的ms是否已经保存了内容.
    可能是你的ms根本没有取到值.
      

  19.   

    插入时把图片数据作为Byte数组插入数据库,下面是把文件转换成数组
    private byte[] ReadImageData(string strFileName)
    {
       byte[] BlobData = null;
       Bitmap picture;
       FileStream stream;
       stream = new FileStream(strFileName,FileModel.Open,FileAccess.Read);
       BlobData = new byte[stream.Length];
       stream.Read(BlobData,0,(int)stream.Length);
       picture = new Bitmap(stream);
       stream.Close();
       return BlobData;
    }从数据库中读取图片文件数据转换成byte数组,下面是如何将数组转换为图片private Bitmap GetBitmap(byte[] img)
    {
       if(img != null)
       {
          MemoryStream ms = new MemoryStream(img,true);
          ms.Read(img,0,img.Length);
          Bitmap bmp = new Bitmap(ms);
          ms.Close();
          return bmp;
       }
       else
          return null;
    }
      

  20.   

    this.oracleConnection1.Open();
    byte[] pic = (byte[])ocm.ExecuteScalar();//这个命令只返回图象数据
    this.oracleConnection1.Close();
    System.IO.MemoryStream ms = new System.IO.MemoryStream(pic);
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
    this.pictureBox1.Image = img;
      

  21.   

    to 我的ms里面有值,我调试看了的,除了这里,那里还有错吗?如果你的ms中有值,就不应该提示这个错误了,
    它还报什么错?
      

  22.   

    public void bindphoto(string xh,PictureBox pictureBox1)
    {
    dbcontroller obj=new dbcontroller();
    string sql="SELECT * FROM photo WHERE xh='" + xh + "'";
    obj.open();
    DataSet myphotods = obj.GetDs(sql);
    obj.close();
    if (myphotods.Tables[0].Rows.Count > 0)
    {
    byte[] bytephoto = new byte[0];
    bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]);
    MemoryStream stmPhoto = new MemoryStream(bytephoto);
    pictureBox1.Image = Image.FromStream(stmPhoto);
    }
    else
    {
    pictureBox1.Image = null;
    }
    }
      

  23.   

    我是用winform作的图片存入数据库,然后通过web显示出来
      

  24.   

    是CS模式的,还是BS模式的程序?
      

  25.   

    哦,这我倒没试过,我这有一个用OPENDIALOG控件(CS模式下)和使用UploadFile控件(BS模式下)的存图片的函数!
    //=============================================================
    // 函 数 名:Savephoto
    // 功能描述:添加或修改照片
    // 输入参数:学生学号,已经打开文件的openFileDialog
    // 返 回 值:无
    //=============================================================
    public void Savephoto(string xh,OpenFileDialog openFileDialog1)
    {
    //定义byte类型变量
    byte[] myblob = null;
    Stream fs;
    //给变量blob赋值
    fs =openFileDialog1.OpenFile();
    if(fs!=null)
    {
    openFileDialog1.Reset();
    myblob = new byte[fs.Length];
    fs.Read(myblob,0,int.Parse(myblob.Length.ToString()));
    fs.Close();
    OracleDataAdapter myoda;
    OracleCommand cmd;
    DataSet myds = new DataSet();
    DataTable mydt = new DataTable();
    DataRow mydr;
    string connstr="User ID=student;Data Source=hres;Password=studentmanage";
    OracleConnection conn= new OracleConnection(connstr);
    string selesql;
    selesql = "SELECT * FROM photo WHERE xh='" + xh + "'";
    cmd = new OracleCommand(selesql,conn);
    myoda = new OracleDataAdapter(selesql,conn);
    myoda.SelectCommand = cmd;
    myoda.Fill(myds, "PHOTO");
    string info = "";
    if (myds.Tables[0].Rows.Count > 0)//修改照片
    {
    info = "修改照片成功";
    string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'";
    OracleCommand updatecmd = new OracleCommand(updatesql,conn);
    myoda.UpdateCommand = updatecmd;
    myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0];
    mydr = mydt.Rows[0];
    string a = mydr["XH"].ToString(); mydr.BeginEdit();
    mydr["XH"] = xh;
    mydr["ZP"] = myblob;
    mydr.EndEdit();
    myoda.Update(myds, "PHOTO");
    }
    else//上传新照片
    {
    info = "上传新照片成功";
    string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)";
    OracleCommand insertcmd = new OracleCommand(insertsql,conn);
    myoda.InsertCommand = insertcmd;
    myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0];
    mydr = mydt.NewRow(); mydr.BeginEdit();
    mydr["XH"] = xh;
    mydr["ZP"] = myblob;
    mydr.EndEdit();
    mydt.Rows.Add(mydr);
    myoda.Update(myds, "PHOTO");
    }
    MessageBox.Show(info);
    }
    }
      

  26.   

    /// 上传图片
    private void SavePhoto(string xh)
    {
    if(IsAllowedExtension(UploadFile))

    //如果是修改图片,则将修改前图片删掉
    string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};
    string path1="";
    //判断该扩展名是否合法
    for(int i = 0; i< arrExtension.Length; i++)
    {
    string fileName1=TextBox_xh.Text;
    path1=Server.MapPath(".") + @"\Photo\" +fileName1+arrExtension[i];
    if(File.Exists(path1))
    {
    FileInfo delete=new FileInfo(path1);
    delete.Delete();
    break;
    }
    }
    //上传图片
    string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToLower();// string extension=".jpg";
    string fileName=TextBox_xh.Text;
    string path = Server.MapPath(".") + @"\Photo\" + fileName + extension;
    //保存图片到服务器目录下
    UploadFile.PostedFile.SaveAs(path);
    //得到图片的二进制形式
    HttpPostedFile upFile=UploadFile.PostedFile;
    //得到上传文件的长度;
    int upFileLength=upFile.ContentLength;
    string ContentType=upFile.ContentType;
    byte[] bPicture;
    bPicture=new byte[upFileLength];
    Stream filestream=upFile.InputStream ;
    filestream.Read (bPicture,0,upFileLength);
    filestream.Close ();
    OracleDataAdapter myoda;
    DataSet myds = new DataSet();
    System.Data.DataTable  mydt = new System.Data.DataTable();
    DataRow mydr;
    OracleCommand cmd;
    string connstr="User ID=student;Data Source=hers;Password=studentmanage";
    OracleConnection conn= new OracleConnection(connstr);
    string selesql = "SELECT * FROM photo WHERE xh='" + xh + "'";
    cmd = new OracleCommand(selesql,conn);
    myoda = new OracleDataAdapter(selesql,conn);
    myoda.SelectCommand = cmd;
    myoda.Fill(myds, "PHOTO");
    string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)";
    string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'";
    try 
    {
    if(myds.Tables[0].Rows.Count > 0)
    {
    OracleCommand updatecmd = new OracleCommand(updatesql,conn);
    myoda.UpdateCommand = updatecmd;
    myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, bPicture.Length, "ZP");
    myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO");
    mydt = myds.Tables[0];
    mydr = mydt.Rows[0];
    string a = mydr["XH"].ToString();
    mydr.BeginEdit();
    mydr["XH"] = xh;
    mydr["ZP"] = bPicture;
    mydr.EndEdit();
    myoda.Update(myds, "PHOTO");
    pictureBox1.ImageUrl=path;
    }
    else
    {
    OracleCommand insertcmd = new OracleCommand(insertsql, conn);
    myoda.InsertCommand = insertcmd;
    myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob,bPicture.Length, "ZP");
    myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO");
    mydt = myds.Tables[0];
    mydr = mydt.NewRow();
    mydr.BeginEdit();
    mydr["XH"] = xh;
    mydr["ZP"] = bPicture ;
    mydr.EndEdit();
    mydt.Rows.Add(mydr);
    myoda.Update(myds, "PHOTO");
    pictureBox1.ImageUrl=path;
    }
    }
    catch
    {
    throw;
    }
    }
    }