1

I am making a face recognition project. I insert a person with face image and it assigned to variable and I use variable in database.

I am inserting image to database and dataGridView. When I insert a image, it writes "Byte[] Array" on dataGridView. How to I convert to image on dataGridView or how to I send to pictureBox ? It is enough one of both.

Variable of face image and convert

 Image img = pictureBox2.Image;
 byte[] arr;
 ImageConverter converter = new ImageConverter();
 arr = (byte[])converter.ConvertTo(img, typeof(byte[]));

İnsert to database

ESG("insert into kisiler values('" + textFaceName.Text + "','" + textFaceSurname.Text + "','" + textNationality.Text + "', '" + int.Parse(textAge.Text) + "','" + cinsiyet + "','"+ arr +"')");
3
  • 1
    FYI your query is vulnerable to SQL injection attacks. Use parameterised queries. Commented Dec 31, 2020 at 10:52
  • To convert an image to a Byte array - take a look at: stackoverflow.com/a/3801289/9365244. Commented Dec 31, 2020 at 10:57
  • Also this is fragile against table columns being reordered Commented Dec 31, 2020 at 11:21

3 Answers 3

2

Simply you can use the following code to convert Byte Array to Image:

Image.FromStream(new MemoryStream(byteArrayIn));

Which you can put it in a function like this:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     Image returnImage = null;
     using (MemoryStream ms = new MemoryStream(byteArrayIn))
     {
         returnImage = Image.FromStream(ms);
     }
     return returnImage;
}

You can also convert your Byte Array to string and use it to bind a PictureBox like the following code. Actually, I've used it in WebApp projects and not sure if it works on yours:

string photo = "data:image/jpeg;base64," + Convert.ToBase64String(byteArray.Photo, 0, byteArray.Photo.Length);
Sign up to request clarification or add additional context in comments.

Comments

1

From PictureBox to DB (VB Code)

Dim fs As FileStream = File.Create("profile.jpg")
ProfilePictureBox.Image.Save(fs, Imaging.ImageFormat.Jpeg)
fs.Close()
fs = File.OpenRead("profile.jpg")
Dim ms As MemoryStream = New MemoryStream
fs.CopyTo(ms)

sqlCmd.Parameters.Add("@profilePic", SqlDbType.VarBinary).Value = ms.ToArray()

From Db To PictureBox (VB Code)

Dim readByte As Byte() = sqlReader("IMG_BYTE")
Dim ms As MemoryStream = New MemoryStream(readByte)
PictureBox1.Image= Image.FromStream(ms)

From Db To DatagridView (VB Code)

Dim img As Image    
Dim readByte As Byte() = sqlReader("IMG_BYTE")
Dim ms As MemoryStream = New MemoryStream(readByte)
img = Image.FromStream(ms)

3 Comments

Please use usings. Incidentally, VarBinary can be passed directly from a stream enclosed in SqlBytes
There's no reason to ever save the image to disk. you can just save it to the memory stream directly.
@Charlieface No, images require that the stream they are created from remains open during the lifetime of the image object. It's... messy.
0

A user named rajantawate1 in this codeproject article provided the following code:

   public byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {
        using (var ms = new MemoryStream())
         {
           imageIn.Save(ms,imageIn.RawFormat);
            return  ms.ToArray();
         }
    }

Comments

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.