2022-03-18 22:48:14 +04:00
|
|
|
|
using ToolsDesktop.Interfaces;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
using System;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2022-03-18 22:48:14 +04:00
|
|
|
|
namespace ToolsDesktop.BaseControls
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Контрол, предоставляющий работу с изображением
|
|
|
|
|
/// </summary>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public partial class BaseControlImage : AbstractBaseControl, IBaseControl
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="propertyName"></param>
|
|
|
|
|
/// <param name="mustFilling"></param>
|
|
|
|
|
/// <param name="readOnly"></param>
|
|
|
|
|
/// <param name="width"></param>
|
|
|
|
|
/// <param name="height"></param>
|
2021-04-03 11:38:27 +04:00
|
|
|
|
public BaseControlImage(string propertyName, bool mustFilling, bool readOnly, int width, int height) : base(propertyName, mustFilling, readOnly)
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
_baseControl = this;
|
|
|
|
|
|
2021-04-03 11:38:27 +04:00
|
|
|
|
if (width != 0)
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
2021-04-03 11:38:27 +04:00
|
|
|
|
Width = width;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
2021-04-03 11:38:27 +04:00
|
|
|
|
if (height != 0)
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
2021-04-03 11:38:27 +04:00
|
|
|
|
Height = height;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buttonLoad.Click += (object sender, EventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
var dialog = new OpenFileDialog();
|
|
|
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pictureBox.ClientSize = new Size(150, 150);
|
|
|
|
|
pictureBox.Image = new Bitmap(dialog.FileName);
|
|
|
|
|
CallOnValueChangeEvent();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке файла", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
panelControl.Controls.Add(pictureBox);
|
2021-04-01 21:30:29 +04:00
|
|
|
|
panelControl.Controls.Add(buttonLoad);
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public void SetDefaultValue() => _originalValue = null;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public void SetValueToControl(object value)
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
2021-04-03 13:41:19 +04:00
|
|
|
|
if (value != null && (value as byte[])?.Length > 0)
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
using MemoryStream mStream = new(value as byte[]);
|
|
|
|
|
pictureBox.Image = Image.FromStream(mStream);
|
|
|
|
|
}
|
|
|
|
|
else if (!_mustFilling)
|
|
|
|
|
{
|
|
|
|
|
pictureBox.Image = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public void DropValueForControl()
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
if (_originalValue != null)
|
|
|
|
|
{
|
|
|
|
|
using MemoryStream mStream = new(_originalValue as byte[]);
|
|
|
|
|
pictureBox.Image = Image.FromStream(mStream);
|
|
|
|
|
}
|
|
|
|
|
else if (!_mustFilling)
|
|
|
|
|
{
|
|
|
|
|
pictureBox.Image = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public bool CheckValueForControl()
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
if (_mustFilling && pictureBox.Image == null)
|
|
|
|
|
{
|
|
|
|
|
BackColor = Color.OrangeRed;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public object GetValueFromControl()
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
if (_mustFilling && pictureBox.Image == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено");
|
|
|
|
|
}
|
|
|
|
|
var converter = new ImageConverter();
|
2021-04-04 21:09:39 +04:00
|
|
|
|
return (byte[])converter.ConvertTo(new Bitmap(pictureBox.Image), typeof(byte[]));
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
|
|
|
|
public string GetPropertyName() => _propertyName;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
|
|
|
|
}
|