4
votes

I have a program to select an image and to set that selected image in a picture box then convert the image in the image box to byte array and save sql server data base in a image type column.

In browse button click event I'm selecting the image file like this.

OpenFileDialog fop = new OpenFileDialog();
fop.InitialDirectory = @"Desktop";
fop.Filter = "image files|*.jpg;*.png;*.gif";
if (fop.ShowDialog() == DialogResult.OK)
{
     FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);  
     pbPartySymbol.Image = new Bitmap(fop.FileName);
     MessageBox.Show("Image Imported Successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

After selecting the image and setting it the picture box image I'm converting the picture box image to byte array in the save button click event and saving the byte array to database.

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

I'm calling the method like this.

byte[] myArr1 = imageToByteArray(pbPartySymbol.Image);

and I'm passing this byte array to the data base. and it saves too. But ALL the added images are saved as like this.* 0x53797374656D2E427974655B5D* Saved images cannot be get back to the picture box in the reading operation. What am I doing wrong in the SAVING?

Here's what I do in saving operation in the form.

            Party ptObj = new Party(myArr1);
            if (new PartyOP().saveParty(ptObj))
            {
                MessageBox.Show("NEW data added");
            }

In my business operation layer this is my code.

          public Boolean saveParty(Party ptObj)
         {
          string query1 = "EXEC insertToParty'" + ptObj.PTSymARR + "'";
           return (new DataAccessLayer().executeNonQueries(query1));
        }

Here's how I have set the property in the Party class.

class Party
{
    public Party() { }
    public Party(byte[] ptSym) 
    {

        this._PTSymARR = ptSym;

    }
    public byte[] PTSymARR
    {
        get { return _PTSymARR; }
        set { _PTSymARR = value; }
    }


}

here's my stored procedure. CREATE PROCEDURE insertToParty ( @ptSymbol image ) AS BEGIN BEGIN TRANSACTION SET NOCOUNT ON; --Query INSERT INTO Party(PTSYM) VALUES (@ptSymbol);

Data type of PTSYM is image data type.

1
Your code seems to be ok, at least as far as i can see. how is the db column defined and how do you read it back, and what happens there? - TaW
@TaW I have a stored procedure to add. I just call it and pass the parameter list. public Boolean saveParty(Party ptObj) { string query1 = "EXEC insertToParty'" + ptObj.PTSymARR + "'"; return (new DataAccessLayer().executeNonQueries(query1)); } Here I'm passing the byte array as object property. The thing is all saved images in the DB looks like this 0x53797374656D2E427974655B5D - chathwind
I'm lost. It's a Byte[], and you're saving it. What do you expect to see? If you want it back to an image later, reverse the process. - DonBoitnott
What is the DB data type, and what DBMS do you use? - TaW
@DonBoitnott Nothing to see . PLEASE READ> ALL the saved image records in the DB shows this record, 0x53797374656D2E427974655B5D. It can't be the same for all the records. Because I didn't save same image. - chathwind

1 Answers

2
votes

That Hex string tranlates to System.Byte[].

You add the Byte-Array to the string starting with EXEC insertToParty so .Net assumes you want a string representation of that variable. Unless specified it calls .ToString wich gives you .... "System.Byte[]".

Edit: Your stored procedure does not use hex encoding on insert. image data shows in hex when viewed in SQL Studio etc.

If you would encode your byte array to a string using base64 or hex when inserting and decode that when you read the data you only had to worry about the increased storage, but I recommend that you change your db acces from a dynamic SELECT to a prepared statement with parameters:

SqlCommand command = new SqlCommand("Exec InsertToParty(@blob)");
command.Parameters.AddWithValue("@blob", ptObj.PTSymARR);
command.ExecuteNonQuery();