Отметки на изображении - C#
Формулировка задачи:
Такой вопрос, необходимо ставить n'ое(много) колличество отметок по определенным координатам на изображении. Отметки могут быть любого вида, это не важно, но необходимо что бы эти отметки можно было удалять и добавлять, так же желательно что бы при наведении на отметку появлялся title. Проблема в том что из-за нехватки опыта не знаю как реализовать.
Все на что хватило ума так это отрисовывать отметки(все) поверх изображения при каждом изменении одной из отметок, но в таком случае просто нереально сделать какой то title для каждой из отметок(.
Прошу вашей помощи, хотя бы указать в какую сторону смотреть)
Возможно мне следует смотреть в сторону opengl?
Решение задачи: «Отметки на изображении»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private readonly List<Marker> _markers = new List<Marker>();
private readonly Тotification _totification = new Тotification();
private bool _isDrawTotification = false;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
Marker.Image = Image.FromStream(new WebClient().OpenRead("https://cdn1.iconfinder.com/data/icons/Map-Markers-Icons-Demo-PNG/128/Map-Marker-Marker-Outside-Azure.png"));
Marker.Size = new Size(64, 64);
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
_markers.Add(new Marker { Location = e.Location, Description = $"Marker №{_markers.Count + 1}" });
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
var marker = _markers.FirstOrDefault(m => m.Contains(e.Location));
if (marker != null)
{
_totification.Description = marker.Description;
_totification.Location = e.Location;
_isDrawTotification = true;
}
else
_isDrawTotification = false;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (var marker in _markers)
marker.Draw(e.Graphics);
if (_isDrawTotification)
_totification.Draw(e.Graphics);
}
}
class Marker
{
public static Image Image { get; set; }
public static Size Size { get; set; }
public Point Location { get; set; }
public string Description { get; set; }
public bool Contains(Point point)
{
return new Rectangle(Location, Image.Size).Contains(point);
}
public void Draw(Graphics graphics)
{
graphics.DrawImage(Image, new Rectangle(Location, Size));
}
}
class Тotification
{
public string Title { get; set; } = "Title";
public string Description { get; set; } = "Description";
public Font Font { get; set; } = new Font("Microsoft Sans Serif", 8F);
public Point Location { get; set; }
public Size Size { get; set; } = new Size(200, 50);
public Color TextColor { get; set; } = Color.Black;
public Color BorderColor { get; set; } = Color.Black;
public Color BackgroundColor { get; set; } = Color.LightYellow;
public bool IsBorderDraw { get; set; } = true;
public void Draw(Graphics graphics)
{
var rectangle = new Rectangle(Location, Size);
using (var brushBackground = new SolidBrush(BackgroundColor))
graphics.FillRectangle(brushBackground, rectangle);
using (var brushText = new SolidBrush(TextColor))
{
graphics.DrawString(Title, Font, brushText, rectangle, new StringFormat { LineAlignment = StringAlignment.Near, Alignment = StringAlignment.Center });
graphics.DrawString(Description, Font, brushText, rectangle, new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Near });
}
if (IsBorderDraw)
using (var penBorder = new Pen(BorderColor))
graphics.DrawRectangle(penBorder, rectangle);
}
}
}