3

I have a SQL Server table with a varbinary(max) column. I use it to store images in it. The images are selected with an OpenFileDialog, translated into a byte[] like this

public byte[] ConvertImageToByteArray(String filepath)
{
    try
    {
        return File.ReadAllBytes(filepath);
    }
    catch (Exception) 
    {
        throw; 
    }
}

and then stored into the database using this line of code:

sqlCmd.Parameters.Add("@image", SqlDbType.VarBinary).Value = image;

It looks like this stored in the database, so I guess all seems to work like expected.

enter image description here

Unfortunately I am unable to load the images back from the datatable.

I am using a SqlDataReader to do so:

DbSql db = new DbSql();

SqlDataReader dr = db.GetDataReader(sqlCmd);

if (dr.Read())
{
    if (!dr.IsDBNull(1))
        productName = dr.GetString(1);

    if (!dr.IsDBNull(2))
        identNumber = dr.GetString(2);

    [...]

    if (!dr.IsDBNull(23))
        comment = dr.GetString(23);

    if (!dr.IsDBNull(24))
    {
        byte[] image = dr.GetSqlBytes(24).Value;  // <- This is where I try to grab the image
    }
}

It seems like I am not able to create a proper byte[] with

image = dr.GetSqlBytes(24).Value;

because my next step is not able to turn it into an image again:

public Image ConvertImageFromByteArray(byte[] array)
{
        try
        {
            MemoryStream ms = new MemoryStream(array);
            return Image.FromStream(ms);
        }
        catch (Exception) { throw; }
    }

EDIT: When trying something like pictureBox.Image = ConvertImageFromByteArray(image) I get an error saying "Invalid parameter" (self translated, saying "Ungültiger Parameter" in german) enter image description here

Anyone able to offer a solution?

7
  • try image = (byte[])dr[24]; Commented Jul 8, 2018 at 10:01
  • Why are you read data using GetString which will corrupt the data. dr.Read() reads one row of the database which contains the entire binary object. So just use : MemoryStream ms = new MemoryStream((byte[])dr.GetValue(23)); which will read the contents of the 24th column. Commented Jul 8, 2018 at 10:22
  • 1
    @Nkosi image is a byte[]. Just added that to my question and also the error message I get. Commented Jul 8, 2018 at 12:55
  • Can you copy the complete error. I am certain it provided more details than that Commented Jul 8, 2018 at 12:58
  • @Nkosi Sorry! Absolutely forgot to upload the picture -.- Commented Jul 8, 2018 at 13:05

1 Answer 1

2

Once you cast your varbinary(MAX) to a byte array

image = (byte[])dr[24];

Try this..

    MemoryStream ms = new MemoryStream(image);
    imagePictureBox.Image = System.Drawing.Image.FromStream(ms);

This is how I create the byte array...

    if (File.Exists(sLogoName) == false)
       throw new Exception("File Not Found: " + sLogoName);
    FileStream sourceStream = new FileStream(sLogoName, FileMode.Open, FileAccess.Read);
    int streamLength = (int)sourceStream.Length;
    Byte[] byLogo = new Byte[streamLength];
    sourceStream.Read(byLogo, 0, streamLength);
    sourceStream.Close();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! The problem was not how I tried to load the image from the database but how I stored it. Using the code you provided to write the byte array I am now able to load the image. Awesome :)
Glad that worked...I had problems with it originally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.