Esempio n. 1
0
        // pobierz: download
        // Rysunek: drawing
        //take a picture from database and convert to Image type
        public System.Drawing.Image GetDrawing(int idOfOID)
        {
            System.Drawing.Image img;

            using (Npgsql.NpgsqlConnection connection = new Npgsql.NpgsqlConnection(GetConnectionString()))
            {
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }

                using (Npgsql.NpgsqlTransaction trans = connection.BeginTransaction())
                {
                    NpgsqlTypes.LargeObjectManager lbm = new NpgsqlTypes.LargeObjectManager(connection);
                    NpgsqlTypes.LargeObject        lo  = lbm.Open(takeOID(idOfOID), NpgsqlTypes.LargeObjectManager.READWRITE); //take picture oid from metod takeOID
                    byte[] buf = new byte[lo.Size()];
                    buf = lo.Read(lo.Size());
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        ms.Write(buf, 0, lo.Size());
                        img = System.Drawing.Image.FromStream(ms);
                    } // End Using ms

                    lo.Close();
                    trans.Commit();

                    if (connection.State != System.Data.ConnectionState.Closed)
                    {
                        connection.Close();
                    }
                } // End Using trans
            }     // End Using connection

            return(img);
        } // End Function GetDrawing
Esempio n. 2
0
        } // End Function GetDrawing

        public System.Drawing.Image GetLargeDrawing(int idOfOID)
        {
            System.Drawing.Image img;

            using (Npgsql.NpgsqlConnection connection = new Npgsql.NpgsqlConnection(GetConnectionString()))
            {
                lock (connection)
                {
                    if (connection.State != System.Data.ConnectionState.Open)
                    {
                        connection.Open();
                    }

                    using (Npgsql.NpgsqlTransaction trans = connection.BeginTransaction())
                    {
                        NpgsqlTypes.LargeObjectManager lbm = new NpgsqlTypes.LargeObjectManager(connection);
                        NpgsqlTypes.LargeObject        lo  = lbm.Open(takeOID(idOfOID), NpgsqlTypes.LargeObjectManager.READWRITE); //take picture oid from metod takeOID
                        byte[] buffer = new byte[32768];

                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                        {
                            int read;
                            while ((read = lo.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                ms.Write(buffer, 0, read);
                            } // Whend

                            img = System.Drawing.Image.FromStream(ms);
                        } // End Using ms

                        lo.Close();
                        trans.Commit();

                        if (connection.State != System.Data.ConnectionState.Closed)
                        {
                            connection.Close();
                        }
                    } // End Using trans
                }     // End lock connection
            }         // End Using connection

            return(img);
        } // End Function GetLargeDrawing