0

i have a hard time from switching from winform to wpf... in winform i just easily read image using picturebox.image

byte[] imgg = (byte[])(reader["profilepic"]); MemoryStream mstream = new MemoryStream(imgg); pic1.Image = System.Drawing.Image.FromStream(mstream);


but in wpf picture box is not available instead an image so i tried the same code just incase it would work

void readName()
    {          
        try
        {
            MySqlConnection conn = new MySqlConnection(myConnection);
            conn.Open();
            MySqlCommand command = new MySqlCommand("SELECT profilepic FROM maindatabase.users where user=?parameter1;", conn);
            command.Parameters.AddWithValue("?parameter1", UserList.SelectedItem.ToString());
            MySqlDataReader reader = command.ExecuteReader();

            //int ctr = 0;
            while (reader.Read())
            {
                //ctr++;
                byte[] imgg = (byte[])(reader["profilepic"]);
               MemoryStream mstream = new MemoryStream(imgg);
               pic1 = System.Drawing.Image.FromStream(mstream);
               //but got an error since system.drawing cant convert system.windows.control.image

               //and after looking in the internet just tried this code but it doesnt seems to work too
                if (imgg != null)
                {
                    using (MemoryStream ms = new MemoryStream(imgg))
                    {
                        // Load the image from the memory stream. How you do it depends
                        // on whether you're using Windows Forms or WPF.
                        // For Windows Forms you could write:
                        // System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
                        imgPic1 = System.Drawing.Image.FromStream(ms);
                    }
                }
                else
                {
                    MessageBox.Show("null");
                }



            }
        }
        catch 
        {
           // MessageBox.Show("error" + ex);

        }

    }


xaml code

<Border BorderThickness="1"
    BorderBrush="#FF000000"
    VerticalAlignment="Center" Margin="10,19,158,33" Height="128">
        <Image Name="pic1"
       Height="128"
       Stretch="Fill"
       VerticalAlignment="Top" Margin="48,-1,39,-1" Width="128"/>
    </Border>


i know winforms and wpf are different so i'll admit that i'm really noob at wpf so if someone can help me..thank you very much

Raymart Calinao
  • 143
  • 1
  • 4
  • 9

2 Answers2

1

WPF does not support Bitmap from System.Drawing namespace. Use some kind of ImageSource like BitmapImage or BitmapSource!

Creating WPF BitmapImage from MemoryStream png, gif

And set this object as Source of Image

Community
  • 1
  • 1
sac1
  • 1,344
  • 10
  • 15
  • tnx for the tips just got codes from here https://codezone4.wordpress.com/2012/06/24/save-and-retrieve-images-in-wpf-from-database/ – Raymart Calinao May 13 '15 at 06:41
0
           var veri = SirketBilgileriData.SirketBilgiListele(); 
           if (veri.SingleOrDefault().Logo == null) { logo.Source =null; } else {
                byte[] kayitliLogo = (byte[])veri.SingleOrDefault().Logo;
                MemoryStream ms = new MemoryStream(kayitliLogo);
                var resimKaynak = new BitmapImage();
                resimKaynak.BeginInit();
                resimKaynak.StreamSource = ms;
                resimKaynak.EndInit();
                logo.Source = resimKaynak;
            }