首页 > 解决方案 > 是否可以在可缩放的图片框图像(固定大小的图片框)上添加标签?

问题描述

我想在工厂地图上标记或图像。当我点击这个标签时,我想打开那个区域的相机。我在工厂地图上的点(标签)应该是可擦的、可移动的、可点击的。使用 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

 
}
}

标签: c#graphicspicturebox

解决方案


推荐阅读