So, I've a class thats imports data from database to my application:
class refresh : MainWindow
{
public refresh(string tableName)
{
connection MyConnection = new connection();
string sqlCommand = "SELECT * FROM " + tableName + "";
DataTable dt_Silo = new DataTable();
MySqlDataAdapter com_Silo = new MySqlDataAdapter(sqlCommand, MyConnection.con);
com_Silo.Fill(dt_Silo);
dataGrid.ItemsSource = dt_Silo.DefaultView;
}
}
I add data and the image by this:
private void buttonProductAdd_Click(object sender, RoutedEventArgs e)
{
try
{
byte[] imageBt = null;
string uriPath = imageProduct.Source.ToString();
string localPath = new Uri(uriPath).LocalPath;
FileStream fstream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
MySqlCommand com_ProductAdd = MyConnection.con.CreateCommand();
com_ProductAdd.CommandText = "INSERT INTO product (NAME, AMOUNT, PICTURE) VALUES('" + textBoxProductAddName.Text + "', '" + textBoxProductAddAmount.Text + "', @IMG)";
MyConnection.con.Open();
com_ProductAdd.Parameters.Add(new MySqlParameter("@IMG", imageBt));
com_ProductAdd.ExecuteNonQuery();
MyConnection.con.Close();
MessageBox.Show("Dodano produkt!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And at last I load the data to my dataGrind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
refresh RefreshProdukty = new refresh("product");
dataGrid.ItemsSource = RefreshProdukty.dataGrid.ItemsSource;
}
How do I display the image instead of "[]Byte Array" ?
You can get the BitmapImage
by using:
private BitmapImage BytesToBitmapImage(byte[] bitmapBytes)
{
using (var ms = new MemoryStream(bitmapBytes))
{
var bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = ms;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.