Convert Images to Binary Style Stream then save to DB.

解决方案 »

  1.   

    ms44(ms44) 
    我从来没有做过跟Image有关的东西,这个之间的转化我不太会的,能给点参考的代码吗?
      

  2.   

    这是我在做项目中用的代码,其中字段意思你看名字就能猜出来:
    //加载item图像
    if(dsMain.Tables["extendimage"].Rows.Count == 1)
    {
    if(dsMain.Tables["extendimage"].Rows[0]["image"] != DBNull.Value)
    {
    byte[] bytePic = (byte[])dsMain.Tables["extendimage"].Rows[0]["image"];
    MemoryStream ms = new MemoryStream(bytePic);
    this.picItem.Image = Image.FromStream(ms);
    ms.Close();
    }
    }
    else
    {
    this.btnClearImage.Enabled = false;
    }
    this.txtImageDescription.DataBindings.Add("Text",dsMain.Tables["extendimage"],"description");以下是添加图像代码:
    private void btnAppendImage_Click(object sender, System.EventArgs e)
    {
    //打开一个对话框,添加item图像信息。
    OpenFileDialog ofdItemImage = new OpenFileDialog();
    ofdItemImage.Title = "Append an image......";
    ofdItemImage.Filter = "(Bitmap|*.bmp;*.dib|JPEG|*.jpeg;*.jpg|GIF|*.gif;*.gfa|Portable Network Graphics|*.png|Windows Metafile|*.wmf|Windows Enhanced Metafile|*.emf";
    ofdItemImage.FilterIndex = 2;
    if(ofdItemImage.ShowDialog() == DialogResult.OK)
    {
    if(ofdItemImage.FileName != null)
    {
    this.picItem.Image = Image.FromFile(ofdItemImage.FileName);

    MemoryStream ms = new MemoryStream();
    picItem.Image.Save(ms,picItem.Image.RawFormat);
    byte[] bytePic = ms.GetBuffer();
    if(dsMain.Tables["extendimage"].Rows.Count == 0)
    {
    DataRow drImage = dsMain.Tables["extendimage"].NewRow();
    drImage["itemid"] = this.ItemID;
    drImage["image"] = bytePic;
    drImage["description"] = "New Image";
    dsMain.Tables["extendimage"].Rows.Add(drImage);
    }
    else
    {
    dsMain.Tables["extendimage"].Rows[0]["image"] = bytePic;
    }
    ms.Close();
    }
    } if(!this.btnClearImage.Enabled)
    {
    this.btnClearImage.Enabled = true;
    this.txtImageDescription.Enabled = true;
    }
    }
      

  3.   

    以下是向oracle数据库中插入图片的代码,access同理
    //byte[] imgdata1
    string mySelectQuery = "INSERT INTO patientpict(pat_no,pat_pict) VALUES(:patno,:imgdata)";
       OracleCommand myCommand = new OracleCommand(mySelectQuery, m_OracleConnection);
       OracleParameter paramPatNo = new OracleParameter("patno", OracleType.VarChar,20 );
       paramPatNo.Value = sPatNo;
       myCommand.Parameters.Add(paramPatNo);
       OracleParameter paramData = new OracleParameter( "imgdata", OracleType.Blob);
       paramData.Value = imgdata1;
       myCommand.Parameters.Add(paramData);
       myCommand.ExecuteNonQuery();
      

  4.   

    public int UploadCaseDoc()
    {
    int result;
    Stream imgStream;
    int imgLen;
    string imgName_value;
    string strDocSuffix;
    string imgUploadedName;
    DBAccess dba = new DBAccess();
    IDbConnection conn = dba.GetConnection();
    dba.OpenConnection(conn);
    IDbTransaction ts = conn.BeginTransaction();
    try
    {
    imgStream  = Request.Files[0].InputStream;
    imgLen =  Request.Files[0].ContentLength;
    imgUploadedName = Request.Files[0].FileName; byte[] imgBinaryData=new byte[imgLen];
    strDocSuffix = Path.GetExtension(imgUploadedName); string strReplace = RevertString(Request.Form["fDocName"].ToString());
    imgName_value = strReplace;
    if(imgName_value.Length < 1)
    imgName_value = Path.GetFileName(imgUploadedName );
    int n = imgStream.Read(imgBinaryData, 0, imgLen); string strTmpFileName = Path.GetFileName(imgUploadedName); result = InsertFile(dba, conn, ts, strTmpFileName, imgBinaryData;

    ts.Commit();

    }
    catch(Exception ex)
    {
    ts.Rollback();
    throw ex;
    }
    finally
    {
    if(conn.State == ConnectionState.Open)
    conn.Close();
    }
    return result;
    }
    public int InsertFile(DBAccess dba, IDbConnection conn, IDbTransaction ts, string strName,byte[] strContent)
    {
    //WSCLBH
    string strSQL="INSERT INTO CONTENT (NAME,CONTENT) VALUES (";
    strSQL += "@name,@content)"; SqlCommand command = new SqlCommand(strSQL, (SqlConnection)conn, (SqlTransaction)ts);
                
                
    SqlParameter param0 =new SqlParameter ("@name", SqlDbType.NVarChar, 128);
    param0.Value = strName;
    command.Parameters.Add(param0);
    SqlParameter param1 =new SqlParameter ("@content", SqlDbType.Image);
    param1.Value = strContent;
    command.Parameters.Add(param1);
    int numRowsAffected = command.ExecuteNonQuery();
    return numRowsAffected;
    }
      

  5.   

    前面还有一部分代码:
    System.IO.FileInfo fi1=new FileInfo (sPicPath);
      int imgdatalen=(int)fi1.Length; 
        byte[] imgdata1 = new byte[imgdatalen];
      Stream imgdatastream=fi1.OpenRead(); 
    int n=imgdatastream.Read(imgdata1,0,imgdatalen);
      

  6.   


    runnercn:
    为什么我一执行下面这个代码,就有异常信息:System.ArgumentException 使用了物料参数??
    if(Reader["Content"] != System.DBNull.Value)
    {
    byte[] bytepic = (byte[])Reader["Content"];
    MemoryStream stream =new MemoryStream(bytepic);
    this.pictureBox1.Image = Image.FromStream(stream,true);
    }为什么了?我的是Access2003的数据库的,Content字段的类型是OLE 对象的,我怎么才能解决了?
      

  7.   

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
      

  8.   

    我写过一个上传附件的控件
    不过是用sqlserver数据库,imag数据类型
    总的来说是将文件按字节读出来,每次4096个字节,然后存入相应的字段
    其实不难,具体你参考一下,streamreader,streamwriter 方法
    更新数据库的时候sqlserver 写的一存储过程如下StringBuilder strB = new StringBuilder(512,1024); strB.Append("CREATE PROCEDURE ").Append(strSpName).Append("  @binData VARBINARY(4096)  AS ");
    strB.Append("DECLARE @ptrField BINARY(16) ");
    strB.Append("SELECT @ptrField = TEXTPTR(").Append(this.DataFieldName).Append(") FROM ").Append(this.TableName).Append(" WHERE ").Append(this.IDFieldName).Append(" = '").Append(this.FileID).Append("'");
    strB.Append("UPDATETEXT ").Append(this.TableName).Append(".").Append(this.DataFieldName).Append(" @ptrField NULL NULL WITH LOG @binData ");