Esempio n. 1
0
        public Image getImagen(int id_voluntario)
        {
            Image   ret    = null;
            CnxBase myBase = new CnxBase();
            string  reqSQL = "SELECT foto FROM z_voluntarios WHERE id_voluntario=" + id_voluntario;

            try
            {
                NpgsqlConnection myConn    = myBase.OpenConnection(myBase.cnxString);
                NpgsqlCommand    myCommand = new NpgsqlCommand(reqSQL, myConn);
                NpgsqlDataReader myReader  = myCommand.ExecuteReader();
                if (myReader.Read() && myReader[0] != DBNull.Value)
                {
                    NpgsqlTransaction t = myConn.BeginTransaction();
                    NpgsqlTypes.LargeObjectManager lbm = new NpgsqlTypes.LargeObjectManager(myConn);
                    NpgsqlTypes.LargeObject        lo  = lbm.Open(Convert.ToInt32(myReader[0]), NpgsqlTypes.LargeObjectManager.READ);
                    byte[] buf = new byte[lo.Size()];
                    buf = lo.Read(lo.Size());
                    MemoryStream ms = new MemoryStream();
                    ms.Write(buf, 0, lo.Size());
                    lo.Close();
                    t.Commit();
                    ret = Image.FromStream(ms);
                }
                myConn.Close();
            }
            catch (Exception myErr)
            {
                throw (new Exception(myErr.ToString() + reqSQL));
            }
            return(ret);
        }
        /// <summary>
        /// Gets the contents of a LargeObject into a buffer.
        /// </summary>
        /// <param name="largeObject">The large object.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev02, 2008-08-08</remarks>
        private byte[] LargeObjectToBuffer(LargeObject largeObject)
        {
            largeObject.Seek(0);
            int size = largeObject.Size();
            byte[] buffer = new byte[size];

            int offset = 0;
            while (offset < size)
            {
                largeObject.Read(buffer, offset, Math.Min(chunkSize, size - offset));
                offset += chunkSize;
            }

            return buffer;
        }