hello hello - 6 months ago 73
SQL Question

How Can i retrieve my Image From Mysql in visual c++/cli

i want to retrieve my image from mysql database the type of it LONG BLOB using visual c++ i try this in my key event

String^ con = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select * from test.testphoto where test.testphoto.name=@name",conn);
MySqlDataReader^ re;
try {
conn->Open();
cmd->Parameters->AddWithValue("@name", "shakira");
re = cmd->ExecuteReader();
while (re->Read()) {
BinaryFormatter^ bf = gcnew BinaryFormatter(); // to convert object to byte array
MemoryStream^ ms = gcnew MemoryStream();
bf->Serialize(ms, re->GetValue(1));
pictureBox1->Image = gcnew Bitmap(ms);
MessageBox::Show("Excute");
}
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();


the problem here that i get error


parameter is not valid


Update

i try this now and same Error parameter is not valid please any help


String^ con = L"datasource=localhost;port=3306;username=root;password=kapookingkong";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select * from test.testphoto where test.testphoto.name='shakira'",conn);
MySqlDataReader^ re;
try {
conn->Open();
re = cmd->ExecuteReader();
re->Read();
BinaryFormatter^ fe = gcnew BinaryFormatter();
MemoryStream^ ms = gcnew MemoryStream();
fe->Serialize(ms, re["photo"]);
array<Byte>^ arr = ms->ToArray();
MemoryStream^ ms2 = gcnew MemoryStream(arr);
pictureBox1->Image = Image::FromStream(ms2);
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();




third try this time index outside bounds of the array Any Help please

this solved i choose the wrong column

String^ con = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select photo from test.testphoto where test.testphoto.name='amr'",conn);
MySqlDataReader^ re;
try {
conn->Open();
re = cmd->ExecuteReader();
array<Byte>^ arr;
while (re->Read())
{
long long l = re->GetBytes(1, 0, nullptr, 0, 0);
arr = gcnew array<Byte>(l);
re->GetBytes(1, 0, arr, 0, l);
}
pictureBox1->Image = Image::FromStream(gcnew MemoryStream(arr));
pictureBox1->Refresh();
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();


i get this code from c# code, i try to modify it so i can use it in c++ i did't find any answer to retrieve image from mysql in visual c++


Note: iam using visual c++ 2015
please i want a clear answer to solve my problem in my code and thanks in advance

Answer

BinaryFormatter^ bf = gcnew BinaryFormatter(); // to convert object to byte array

The object contains a byte array. Converting an object containing a byte array to a byte array results in something other than the original byte array. You can verify that yourself by making a simple example of creating a byte array, serializing it, and then inspecting the contents of the memorystream (which will now be larger)

I think you need to use GetBytes() on the reader. You can read more at https://msdn.microsoft.com/en-us/library/87z0hy49%28v=vs.110%29.aspx and MySqlDataReader GetBytes buffer issue...