c# - 是否可以在可缩放的图片框图像(固定大小的图片框)上添加标签?
问题描述
我想在工厂地图上标记或图像。当我点击这个标签时,我想打开那个区域的相机。我在工厂地图上的点(标签)应该是可擦的、可移动的、可点击的。使用 DrawImage 是不可能做到这一点的。我不想使用图形绘制图像作为替代方式,而是想创建一个动态标签并影响固定大小的图片框的图像。但我不能将“pictureBox.Image”分配给标签的父属性。
我在哪里犯错。图片框应该是固定的,其中的图片应该是可缩放的。但是标签的作用与图像是分开的。我请求你的帮助。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Cyotek.Windows.Forms.Properties;
namespace Cyotek.Windows.Forms
{
internal partial class ScaledAdornmentsDemoForm : BaseForm
{
#region Instance Fields
//private List<Point> _landmarks;
private List<Label> _landmarks;
private Bitmap _markerImage;
#endregion
#region Public Constructors
public ScaledAdornmentsDemoForm()
{
InitializeComponent();
}
#endregion
#region Overridden Methods
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
imageBox.ZoomToFit();
_markerImage = Resources.circle_24;
_landmarks = new List<Label>();
this.AddLabel(new Label() { Text = "", BackColor = Color.Transparent, Location = new Point(467, 447), Parent = imageBox, Image = _markerImage });
this.AddLabel(new Label() { Text = "", BackColor = Color.Transparent, Location = new Point(662, 262), Parent = imageBox, Image = _markerImage });
this.AddLabel(new Label() { Text = "", BackColor = Color.Transparent, Location = new Point(779, 239), Parent = imageBox, Image = _markerImage });
}
#endregion
#region Private Members
private void AddLabel(Label label)
{
Debug.Print("Added label: {0}", label);
//label.Location = new Point(label.Location.X - (label.Size.Width / 2), label.Location.Y - _markerImage.Size.Height);
// imageBox.Controls.Add(label);
_landmarks.Add(label);
}
#endregion
#region Event Handlers
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
//AboutDialog.ShowAboutDialog();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void imageBox_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (imageBox.IsPointInImage(e.Location))
{
this.AddLabel(new Label() { Text = "", BackColor = Color.Transparent, Location = e.Location, Parent = imageBox, Image = _markerImage });
}
}
}
private void imageBox_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine();
foreach (Label label in _landmarks)
{
label.Location = imageBox.GetOffsetPoint(label.Location);
//imageBox.Controls.Add(label);
//e.Graphics.DrawImage(_markerImage, new Rectangle(location, markerSize), new Rectangle(Point.Empty, _markerImage.Size), GraphicsUnit.Pixel);
}
}
#endregion
}
}
解决方案
推荐阅读
- reactjs - 如何使用单个按钮在 React js 中隐藏和显示组件
- python - 查看包含您姓名的唯一字母的列表(不包含空格键)和包含出现次数等的列表
- html - 如何将圆形边框分为五个部分
- c++ - 为什么在包含 QStackedWidget-inherited 类后 Qt 不能构建我的项目?
- python - Python 在指定的 GMT 获取时间
- jenkins - 詹金斯计划的备份管道 - 确保所有作业在运行前停止
- linux - 如何加快 SMP 机器上的编译过程?
- windows - 如何使用 Powershell 将 curl 命令的结果通过管道传输到 tar?
- google-cloud-automl - 使用 AutoML 进行不同语言对的机器翻译
- android - 如何在运行时更新 Android BLE Advertiser 服务数据?