PIAPS_CW/WinFormsApp/FormMediaFiles.cs

151 lines
5.1 KiB
C#

using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class FormMediaFiles : Form
{
private readonly ILogger _logger;
private readonly IMediaFileLogic _mediafileLogic;
private readonly IProductLogic _productLogic;
private byte[]? _image;
public FormMediaFiles(ILogger<FormMediaFiles> logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
{
InitializeComponent();
_logger = logger;
_mediafileLogic = mediaFileLogic;
_productLogic = productLogic;
_image = null;
}
private void LoadData()
{
if (comboBoxProduct.SelectedIndex < 0) return;
_logger.LogInformation("Загрузка файлов");
try
{
var filelist = _mediafileLogic.ReadList(null);
if (filelist != null)
{
dataGridView.DataSource = filelist;
}
}
catch (Exception ex)
{
}
var list = _mediafileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = (Guid?)comboBoxProduct.SelectedValue ?? Guid.Empty,
});
panelFiles.Controls.Clear();
if (list.Count != 0)
{
foreach (var item in list)
{
var imageBox = new PictureBox();
imageBox.Dock = DockStyle.Top;
using (var ms = new MemoryStream(item.Image))
{
Image image = Image.FromStream(ms);
imageBox.Image = image;
}
imageBox.Width = groupBox1.Width;
panelFiles.Controls.Add(imageBox);
imageBox.SizeMode = PictureBoxSizeMode.Zoom;
imageBox.Height = 400;
imageBox.BorderStyle = BorderStyle.FixedSingle;
var imageLabel = new Label();
imageLabel.Dock = DockStyle.Top;
imageLabel.TextAlign = ContentAlignment.MiddleCenter;
panelFiles.Controls.Add(imageLabel);
panelFiles.Refresh();
}
}
}
private void FormMediaFiles_Load(object sender, EventArgs e)
{
try
{
var list = _productLogic.ReadList(null);
if (list != null)
{
comboBoxProduct.DisplayMember = "Name";
comboBoxProduct.ValueMember = "Id";
comboBoxProduct.DataSource = list;
comboBoxProduct.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка продуктов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBoxProduct_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (comboBoxProduct.SelectedIndex != -1 && _image is not null)
{
_mediafileLogic.Create(new MediaFileBindingModel()
{
Id = Guid.NewGuid(),
Image = _image,
ProductId = (Guid)comboBoxProduct.SelectedValue,
});
dataGridView.Update();
panelFiles.Refresh();
}
}
private void buttonSelectFile_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read))
{
_image = new byte[fs.Length];
fs.Read(_image, 0, (int)fs.Length);
}
labelFile.Text = Path.GetFileName(dialog.FileName);
}
}
private void FormMediaFiles_Resize(object sender, EventArgs e)
{
groupBoxFiles.Width = Convert.ToInt32(this.Width * 0.4);
panelFiles.Height = this.Height;
foreach (var control in this.Controls.OfType<PictureBox>())
{
control.Width = panelFiles.Width;
control.Height = Convert.ToInt32(panelFiles.Height * 0.3);
}
}
}
}