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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

tnx for the tips just got codes from here codezone4.wordpress.com/2012/06/24/…
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;
            }

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.