Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dudes
i am working on face detection windows form application first user register and then second time he will come the application will recognize him i am using (Emgu) image processing , i got code from internet it is working but i want to save image in database , for recognization it will bring image from database i am trying to save detected object like that



C#
public static void SaveImage(Image<gray,> imsgArray)
{
try {
string query = "INSERT INTO Face_Detection(imageContent) VALUES (@payload)";
SqlCommand command = new SqlCommand(query, sqlConnection);
command.Parameters.AddWithValue("@payload", SqlDbType.Binary);
command.Parameters["@payload"].Value = imsgArray.Data;  (  contain Data like  imsgArray.Data{[byte[100,100,1]]})
command.ExecuteNonQuery();
}
catch (Exception ex) {
throw ex;
}
finally { }
}


and i am getting exception below can any body help please.
Note: In database column type is binary for more clear please find attached image.
No mapping exists from object type System.Byte[,,] to a known managed provider native type.
Posted
Updated 19-Nov-15 1:16am
v2

1 solution

The error is self explanatory:
C#
No mapping exists from object type System.Byte[,,] to a known managed provider native type.

You can't just throw a 3D array at an SQL Binary field and expect it to work out what to do with it: it just stores data, it doesn't work with higher level constructs such as multidimensional arrays. So when you try, the framework looks at your imsgArray.Data and can't convert it to a one dimensional array of bytes automatically.
C#
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, imsgArray.Data);
byte[] data = ms.ToArray();
You can then pass the data to SQL, and unserialize it when you retrieve it.
 
Share this answer
 
Comments
Khudha Bakhsh Mari 19-Nov-15 7:43am    
Thanks Dear For answering now it is inserting successfully in database but when i am getting it and deserializing getting (End of Stream encountered before parsing was completed.) exception
OriginalGriff 19-Nov-15 7:54am    
Show the code you use to serialise and store, and to retrieve and deserialize.
Khudha Bakhsh Mari 19-Nov-15 8:41am    
//Start Serializing
public static void SaveImage(Image<gray, byte=""> imsgArray)
{
try
{
GetConnectionSting("");
string query = "INSERT INTO Face_Detection(imageContent) VALUES (@payload)";
SqlCommand command = new SqlCommand(query, sqlConnection);
command.Parameters.AddWithValue("@payload", SqlDbType.Binary);


BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, imsgArray.Data);
command.Parameters["@payload"].Value = ms.ToArray();
command.ExecuteNonQuery();
CloseConnection();
}
catch (Exception ex)
{
throw ex;
}
finally { }
}
//End Serializing

//Start Getting Image From Database and Desalinize
public static Image<gray, byte=""> GetImages()
{
try
{
Image<gray, byte=""> img = null;
Image bt = null;
GetConnectionSting("");
using (SqlCommand command = new SqlCommand("select * from Face_Detection", sqlConnection))
{
//
// Invoke ExecuteReader method.
//
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
byte[] barray = (byte[])reader["imageContent"];
// convert byte array to memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(barray);
// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// set memory stream position to starting point
_MemoryStream.Position = 0;
byte[, ,] threeDimensional = new byte[1000, 1000, 1000];
_MemoryStream.Seek(0, SeekOrigin.Begin);
var object=_BinaryFormatter.Deserialize(_MemoryStream);

}
}
var image = new Image<bgr, byte="">(new Bitmap(bt));

//string query = "INSERT INTO Face_Detection(imageContent) VALUES (@payload)";
//SqlCommand command = new SqlCommand(query, sqlConnection);
//command.Parameters.Add("@payload", imsgArray.Bytes);
//command.ExecuteNonQuery();

return img;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
}

}
//End Getting image from Database and Deseriazlize
OriginalGriff 19-Nov-15 8:57am    
And what does the debugger show is in the data and in "object"?
Did you remove all the old values from the DB first?
Check your serialize / deserialize first: serialise the image data to an byte array, then immediately deserialize it to a new 3D array. Are the two 3D arrays identical in size and content?
Khudha Bakhsh Mari 19-Nov-15 16:25pm    
Dear i am confused when i am capturing image and getting captured image as ( Image <bgr, byte> ) object, so object contain more than one property and which i have to store in database and also i have to retrieve stored image and compare with detected image so what should i do please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900