Save all images (BLOB) from database to folder ('Out of memory.')

Viewed 164

I have a SQLite database with a table have a good amount if images, and I got some problems in database size, So I converted the logic to store the images in a folder and retrieve the needed images, so what I'm doing now it trying to get all the images stored on the database and save it in folder but I'm getting an error Out of memory.

Note: I used a compress method to compress the image before insert it to the database and also using decompress method when retrieve the image.

here is my code :

protected void Save()
    {
        try
        {
            using (SQLiteDataReader reader = DBConn.ExecuteReader("Select ID,image from transfers", CommandType.Text))
            {
                while (reader.Read())
                {
                    if (reader["image"] != DBNull.Value)
                    {
                        byte[] image = (byte[])reader["image"];
                        image = Decompress(image);
                        Image newImage = byteArrayToImage(image);

                        string path = string.Format(@"E:\images\CI-{0}.Jpeg", reader["ID"].ToString());
                        newImage.Save(path, ImageFormat.Jpeg);
                    }
                }
            }
            MessageBox.Show("done");
        }
        catch (SQLiteException ex)
        {
            // Do some logging or something. 
            MessageBox.Show("There was an error accessing your data. DETAIL: " + ex.Message);
        }
    }

method to get image from byte

public Image byteArrayToImage(byte[] bytesArr)
    {
        Bitmap newBitmap;
        using (MemoryStream memoryStream = new MemoryStream(bytesArr))
        {
            using (Image newImage = Image.FromStream(memoryStream))
            {
                newBitmap = new Bitmap(newImage);
            }
        }
        return newBitmap;

        // I tried this code also.
        //return (Bitmap)((new ImageConverter()).ConvertFrom(bytesArr));
    }

And this is the Decompress method

public static byte[] Decompress(byte[] data)
    {
        using (MemoryStream input = new MemoryStream(data))
        using (MemoryStream output = new MemoryStream())
        {
            using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
            {
                dstream.CopyTo(output);
            }
            return output.ToArray();
        }
    }
1 Answers

I Changed the code from the advices I got from the comments so Thank you.

here is the final code:

 protected void Save()
    {
        try
        {
            using (SQLiteDataReader reader = DBConn.ExecuteReader("Select ID,image from transfers", CommandType.Text))
            {
                while (reader.Read())
                {
                    if (reader["image"] != DBNull.Value)
                    {
                        byte[] image = (byte[])reader["image"];
                        image = Decompress(image);

                        string path = string.Format(@"E:\images\CI-{0}.jpeg", reader["ID"].ToString());
                        File.WriteAllBytes(path, image);
                    }
                }
            }
            MessageBox.Show("done");
        }
        catch (SQLiteException ex)
        {
            // Do some logging or something. 
            MessageBox.Show("There was an error accessing your data. DETAIL: " + ex.Message);
        }
    }

I changed the Decompress method as bellow :

    public static byte[] Decompress(byte[] inputData)
    {
        if (inputData == null)
            throw new ArgumentNullException("inputData must be non-null");

        MemoryStream input = new MemoryStream(inputData);
        MemoryStream output = new MemoryStream();
        using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
        {
            dstream.CopyTo(output);
        }
        return output.ToArray();

        if (inputData == null)
            throw new ArgumentNullException("inputData must be non-null");
    }
Related