我如何从wpf中的数据库中检索图像

时间:2022-09-03 23:14:55

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

我很难从winform切换到wpf ...在winform中我只需使用picturebox.image轻松读取图像

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

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

但在wpf图片框不可用而不是图像,所以我尝试相同的代码只是因为它会工作

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

我知道winforms和wpf是不同的,所以我承认我在wpf真的很棒,所以如果有人可以帮助我......非常感谢你

2 个解决方案

#1


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

WPF不支持System.Drawing命名空间中的Bitmap。使用像BitmapImage或BitmapSource这样的ImageSource!

Creating WPF BitmapImage from MemoryStream png, gif

从MemoryStream png,gif创建WPF BitmapImage

And set this object as Source of Image

并将此对象设置为图像源

#2


           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;
            }

#1


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

WPF不支持System.Drawing命名空间中的Bitmap。使用像BitmapImage或BitmapSource这样的ImageSource!

Creating WPF BitmapImage from MemoryStream png, gif

从MemoryStream png,gif创建WPF BitmapImage

And set this object as Source of Image

并将此对象设置为图像源

#2


           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;
            }