0

I need to retrieve an image from a database and save it to disk. In the database, the image is stored in binary format but datatype of the column is varchar(5000).

This is the code that I am using for retrieving the image and saving it to disk

public void CreateImageDataUsingDataReader_ForNetezzaDB()
{
        string strDbConn = string.Empty;
        string strImageFileName = string.Empty;
        string strImageData = string.Empty;
        string strImgSavePath = string.Empty;
        string strQuery = string.Empty;

        Byte[] byteImageData;
        MemoryStream stmImageData = new MemoryStream();
        Image saveImage;

        try
        {
            //---open the database connection
            strDbConn = ConfigurationSettings.AppSettings["NetezzaDBConnection"].ToString().Trim();
            OleDbConnection dbcon = new OleDbConnection(strDbConn);
            dbcon.Open();
            strQuery = "select name,signature_vod__c from sfb_call2_vod where signature_vod__c is not null  limit 10";
            OleDbCommand cmdSelect = new OleDbCommand(strQuery, dbcon);
            OleDbDataReader imageReader = cmdSelect.ExecuteReader();

            if (imageReader.HasRows)
            {
                while (imageReader.Read())
                {
                    strImageFileName = imageReader["name"].ToString().Trim();
                    strImageData = imageReader["signature_vod__c"].ToString().Trim();

                    stmImageData.Seek(0, SeekOrigin.Begin);
                    //converting string to byte array
                    byteImageData = Convert.FromBase64String(strImageData);
                    //---create Memory stremm from the Image Byte data                        
                    stmImageData.Write(byteImageData, 0, byteImageData.Length);
                    //--saving the image
                    //saveImage = Image.FromStream(stmImageData);
                    using (saveImage = Image.FromStream(stmImageData))
                    {
                        strImgSavePath = ConfigurationSettings.AppSettings["ImageSavePath"].ToString().Trim();
                        saveImage.Save(strImgSavePath + strImageFileName + ".png", System.Drawing.Imaging.ImageFormat.Png);   ///---error comes in this line                           
                    }
                }
            }
            imageReader.Close();
            dbcon.Close();
            stmImageData.Close();
            stmImageData = null;
        }
        catch (Exception ex)
        {
            throw new Exception("Error Occured in method CreateImageDataUsingDataReader " + ex.Message);
        }
}

but I keep getting an error:

A generic error occurred in GDI+.

Same code if I execute for SQL Server database it works fine but issue comes only with the Netezza database

Please help me resolve this issue

1
  • Remove your try ... catch clause, it is hiding errors. Nov 28, 2012 at 20:22

1 Answer 1

0

You mention that you store the binary image in a varchar column. This and the fact that it works on an other db technology makes it obious that you read different data back in the Netazza case.

I would suggest to setup a testproject where you persist the same image to the 2 different databases (netazza and mssql), read it back from both a do a bitwise comparison of the result either between the db results or between original and read from db.

I would be surprised if you get the same result and if I am right, you should probalaby consider to use a binary data type to persist the image data in your db backend.

1
  • Agreed - you should be saving them in binary form. Images tend to take up a lot of space anyway, and by base64 encoding them so you can get them in a varchar column you are adding 1/3rd to the size of each one.
    – Kevin
    Nov 28, 2012 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.