Picture box to byte array

耗尽温柔 提交于 2019-12-05 00:51:40

问题


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:


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();


来源:https://stackoverflow.com/questions/26405961/picture-box-to-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!