PIAPS_CW/WinFormsApp/FormMediaFiles.cs

151 lines
5.1 KiB
C#
Raw Normal View History

2024-06-24 16:23:42 +04:00
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
{
2024-06-24 16:23:42 +04:00
private readonly ILogger _logger;
private readonly IMediaFileLogic _mediafileLogic;
private readonly IProductLogic _productLogic;
2024-06-25 20:26:34 +04:00
private byte[]? _image;
2024-06-24 16:23:42 +04:00
public FormMediaFiles(ILogger<FormMediaFiles> logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
{
InitializeComponent();
2024-06-24 16:23:42 +04:00
_logger = logger;
_mediafileLogic = mediaFileLogic;
_productLogic = productLogic;
2024-06-25 20:26:34 +04:00
_image = null;
2024-06-24 16:23:42 +04:00
}
private void LoadData()
{
if (comboBoxProduct.SelectedIndex < 0) return;
2024-06-24 17:09:03 +04:00
_logger.LogInformation("Загрузка файлов");
try
{
var filelist = _mediafileLogic.ReadList(null);
if (filelist != null)
{
dataGridView.DataSource = filelist;
}
}
catch (Exception ex)
{
}
2024-06-24 16:23:42 +04:00
var list = _mediafileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = (Guid?)comboBoxProduct.SelectedValue ?? Guid.Empty,
});
panelFiles.Controls.Clear();
2024-06-25 20:26:34 +04:00
if (list.Count != 0)
2024-06-24 16:23:42 +04:00
{
2024-06-25 20:26:34 +04:00
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();
}
2024-06-24 16:23:42 +04:00
}
}
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)
{
2024-06-25 20:26:34 +04:00
if (comboBoxProduct.SelectedIndex != -1 && _image is not null)
2024-06-24 16:23:42 +04:00
{
_mediafileLogic.Create(new MediaFileBindingModel()
{
Id = Guid.NewGuid(),
2024-06-25 20:26:34 +04:00
Image = _image,
2024-06-24 16:23:42 +04:00
ProductId = (Guid)comboBoxProduct.SelectedValue,
});
2024-06-25 20:26:34 +04:00
dataGridView.Update();
panelFiles.Refresh();
2024-06-24 16:23:42 +04:00
}
2024-06-25 20:26:34 +04:00
2024-06-24 16:23:42 +04:00
}
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)
{
2024-06-25 20:26:34 +04:00
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);
2024-06-24 16:23:42 +04:00
}
}
private void FormMediaFiles_Resize(object sender, EventArgs e)
{
2024-06-24 17:09:03 +04:00
groupBoxFiles.Width = Convert.ToInt32(this.Width * 0.4);
2024-06-24 16:23:42 +04:00
panelFiles.Height = this.Height;
foreach (var control in this.Controls.OfType<PictureBox>())
{
control.Width = panelFiles.Width;
control.Height = Convert.ToInt32(panelFiles.Height * 0.3);
}
}
}
}