Reputation: 103
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 +"')");
Upvotes: 1
Views: 6703
Reputation: 1
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();
}
}
Upvotes: 0
Reputation: 4829
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);
Upvotes: 2
Reputation: 19
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)
Upvotes: 1