KOP-PIbd-32-Katysheva-N-E/ComponentsLibrary/ImageLoad.cs

57 lines
1.0 KiB
C#
Raw Permalink Normal View History

2024-09-16 20:52:42 +04:00

namespace ComponentsLab
{
public partial class ImageLoad : UserControl
{
private event EventHandler? _avatarChanged;
private event Action? _errorOccured;
public string Error { get; private set; }
public Image Avatar
{
get
{
return pictureBoxAvatar.Image;
}
set
{
pictureBoxAvatar.Image = value;
}
}
public event EventHandler AvatarChanged
{
add { _avatarChanged += value; }
remove { _avatarChanged -= value; }
}
public event Action AnErrorOccurred
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
public ImageLoad()
{
InitializeComponent();
Error = string.Empty;
}
private void buttonLoad_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
pictureBoxAvatar.Image = Image.FromFile(ofd.FileName);
_avatarChanged?.Invoke(this, e);
}
catch (Exception ex)
{
Error = ex.Message;
_errorOccured?.Invoke();
}
}
}
}
}