我很难从winform切换到wpf ...在winform中我只需使用picturebox.image轻松读取图像
byte[] imgg = (byte[])(reader["profilepic"]);
MemoryStream mstream = new MemoryStream(imgg);
pic1.Image = System.Drawing.Image.FromStream(mstream);
但是在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代码
<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>
我知道winforms和wpf是不同的所以我承认我在wpf真的很棒,所以如果有人可以帮助我......非常感谢你
答案 0 :(得分:1)
WPF不支持System.Drawing
命名空间中的位图。
使用某种ImageSource
BitmapImage
或BitmapSource
!
Creating WPF BitmapImage from MemoryStream png, gif
并将此对象设置为Source
Image
答案 1 :(得分: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;
}