c reads the image and saves it to the database

  • 2020-06-03 08:05:13
  • OfStack


 note :MyTools.g_PhotoField Is the name of the image field in the database table 
// Save the image to the database 
    if(this.picPhoto.Image==null)
    {
     m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
    }
    else
    {
     try 
     {
      MemoryStream ms = new MemoryStream ();
      picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
      byte [] myData = new Byte [ms.Length ];
      ms.Position = 0;
      ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
      m_DataRow[MyTools.g_PhotoField] = myData;
     }
     catch(System.Exception ee) 
     {
      MessageBox.Show(ee.Message);
     }
    }//else
// Read the image 
    if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
    {
     try
     {
      Byte[] byteBLOBData =  new Byte[0];
      byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
      MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
      this.picPhoto.Image= Image.FromStream(stmBLOBData);
     }
     catch(Exception ex)
     {
      MessageBox.Show(ex.Message);
     }
    }
    else
    {
     this.picPhoto.Image= null;
    }


Related articles: