0

i'm a student and i am bad at programing. I saved the images in my mysql database for each player. I created a program where I can list some soccer players from my database. When i click on a listed player in datagrid, a new window appears with the information about the player. Everything works, but now i want a picture of the selected player to be displayed on the information window from the database. Can anybody help me? My english is not the best (i'm 17) so i hope you can understand what i mean.

This is what i tried to do but i don't know how to continue. PS. It's in WPF.

 MySqlCommand cmd = new MySqlCommand("SELECT Bilder FROM spieler WHERE Bilder='{8}'");
        MySqlDataReader rdr1 = cmd.ExecuteReader();

        try
        {
            conn.Open();
            while (rdr1.Read())
            {
                // image1... I don't know what to write here
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Fehler: " + ex);
        }

        rdr1.Close()

2 Answers 2

2

Just get it using a byte[] casting beforehand:

while (rdr1.Read())
{
   byte[] data = (byte[])reader[0]; // 0 is okay if you only selecting one column
   //And use:
   using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
   {
      Image image = new Bitmap(ms);
   }
}

UPDATE: In WPF, use the BitmapImage:

using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
    var imageSource = new BitmapImage();
    imageSource.BeginInit();
    imageSource.StreamSource = ms;
    imageSource.CacheOption = BitmapCacheOption.OnLoad;
    imageSource.EndInit();

    // Assign the Source property of your image
    yourImage.Source = imageSource;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but in this line: Image image = new Bitmap(ms); i get an error, it says that 'Bitmap' could not be found. Any idea?
@user3718026 Yes, your object don't have a reference for the assembly which should hold the Bitmap class. Since you're using WPF, just add the appropiate image to your window. See my update.
One thing missing. You have to set imageSource.CacheOption = BitmapCacheOption.OnLoad; (before EndInit) in order to make the bitmap load before the stream is closed. It may work without that option on a MemoryStream, but only by accident. Setting this option is required when you want to close an image stream immediately.
@YairNevet I have one more question, I get an error at data in this line: using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data)) It says that data is not in the context. Thank you in advance.
0

What is column's type where you hold the image? You could try somehing like this

Image tmp = ImageConverter.ConvertFrom(rdr1.GetStream("photo"));

where photo is name of your column

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.