0

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 didn't find any answer to retrieve image from MySQL in visual C++

Note: I'm using visual C++ 2015

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

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...

Community
  • 1
  • 1
zeromus
  • 1,648
  • 13
  • 14
  • ok but when i use GyteByte of MysqlDataReader i get specified cast is not valid i change the code after open connection by that –  May 07 '16 at 19:11
  • re = cmd->ExecuteReader(); re->Read(); array^ arr = gcnew array(re->GetByte(1)); pictureBox1->Image = Image::FromStream(gcnew MemoryStream(arr)); –  May 07 '16 at 19:11
  • 1
    What do you expect to happen? You called GetByte(). You think your picture fits in one byte? Who told you to use GetByte(), anyway? – zeromus May 08 '16 at 04:43
  • please write the full syntax of it on how to use getbytes i make search on it all i found is c# codes and put the code in the answer and thank you –  May 08 '16 at 13:16
  • What's wrong with c# code? Converting that to C++/CLI is simple. – zeromus May 08 '16 at 16:30
  • i get some error when using GetBytes Please Give me the example or how to write it in my code i will appreciate and thank you –  May 08 '16 at 16:35
  • Nope. I need to see what you've tried. Add an update to your question. – zeromus May 08 '16 at 18:35
  • 1
    OK, after pasting the code, you need to think about it. Understand what each bit of it means. You're telling it to use column 1 because that's what the example did, because the example did `select pub_id, logo`. Do you select two things, with the image as column[1]? – zeromus May 11 '16 at 21:33
  • no i select only the photo where the name is equal to 'amr' wich will found in col[0] –  May 11 '16 at 23:11
0

To fix the issue you need to add the following line at the top of your file:

using namespace System::IO;
Dharman
  • 30,962
  • 25
  • 85
  • 135