TransWikia.com

Is it possible to add a label on the zoomable picturebox image(fixed-size picturebox)?

Stack Overflow Asked on December 30, 2021

I want to label or image on a factory map. When I click on this tag, I want to open the camera in that region. My points (labels) on the factory map should be erasable, movable, clickable. It is impossible to do this with DrawImage. Instead of using graphic draw image as an alternative way, I want to create a dynamic label and affect the image of the fixed size picturebox. But I can’t assign ‘pictureBox.Image’ to the parent property for the label.

Where am I making mistakes. The picture box should be fixed and the picture in it should be zomable. But the label acts separately from the image. I request your help.

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

 
}
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP