private void Button_Click_1(object sender, RoutedEventArgs e) { cat.EnableValidation = true; if (string.IsNullOrEmpty(cat.Error)) { if (!string.IsNullOrEmpty(file_selected)) { string ext = Path.GetExtension(file_selected); string fileName = Path.GetRandomFileName() + ext; string fileSavePath = Path.Combine(Directory.GetCurrentDirectory(), "images", fileName); var bmp = ResizeImage.ResizeOrigImg( new Bitmap(System.Drawing.Image.FromFile(file_selected)), 75, 75); bmp.Save(fileSavePath, ImageFormat.Jpeg); file_name = fileSavePath; } var cat_info = new AppCat { Name = tbname.Text, Birth = (DateTime)bdcat.SelectedDate, Description = tbdesc.Text, Gender = cbitem.Text.ToString(), Image = file_name }; cat_info.AppCatPrices = new List <AppCatPrice> { new AppCatPrice { CatId = cat_info.Id, DateCreate = DateTime.Now, Price = decimal.Parse(tbprice.Text) } }; _cats.Add(new CatVM { Id = cat_info.Id.ToString(), Name = cat_info.Name, Birthday = cat_info.Birth, Description = cat_info.Description, Image = cat_info.Image }); _context.Add(cat_info); _context.SaveChanges(); Close(); } else { MessageBox.Show(cat.Error); } }
private void Button_Click(object sender, RoutedEventArgs e) { var cat_item = _context.Cats .SingleOrDefault(p => p.Id == _res); //якщо обрали файл з фото кота. if (!string.IsNullOrEmpty(file_select)) { //розширення файла. string ext = System.IO.Path.GetExtension(file_select); //імя з розширенням файла. string fileName = System.IO.Path.GetRandomFileName() + ext; //шлях до файла повний. string fileSavePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "images", fileName); //змінюємо розмір фото. var bmp = ResizeImage.ResizeOrigImg( new Bitmap(System.Drawing.Image.FromFile(file_select)), 75, 75); //зберігаємо фото з вже вказаним розміром. bmp.Save(fileSavePath, ImageFormat.Jpeg); //заносимо в базу нове фото. cat_item.Image = fileSavePath; _context.SaveChanges(); } if (!string.IsNullOrEmpty(tbnewname.Text)) { if (!cat.Error.Equals("Недопустимі цифри і латиниця!")) { MessageBox.Show("true"); cat_item.Name = tbnewname.Text; _context.SaveChanges(); } } if (decimal.Parse(tbprice.Text) > 0) { if (!cat.Error.Equals("Повинно бути більше нуля!")) { cat_item.AppCatPrices = new List <AppCatPrice> { new AppCatPrice { CatId = cat_item.Id, DateCreate = DateTime.Now, Price = decimal.Parse(tbprice.Text) } }; _context.SaveChanges(); } } if (dpdatebirth.SelectedDate != null) { if (!cat.Error.Equals("Некоректна дата!")) { cat_item.Birth = (DateTime)dpdatebirth.SelectedDate; _context.SaveChanges(); } } else { MessageBox.Show("NoData"); } if (!string.IsNullOrEmpty(tbdes.Text)) { cat_item.Description = tbdes.Text; _context.SaveChanges(); } }