How do I control the position of a label or any PictureBox?
I am doing a successful control (shortcut or image) in a PictureBox. When I navigate it will keep the control position (x, y).
Like this:
But the problem is this: the result of the image is:
In front of the gif image, I dragged a label control on the center screen. But the result image does not keep the mark in the center screen.
I have set the PictureBox attribute to StretchImage.
My code to get the position and DrawText in the PictureBox, for example:
public PositionControl CtrlPos = new PositionControl();
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control control = (Control)sender;
Point nextPosition = new Point();
nextPosition = picPreview.PointToClient(MousePosition);
nextPosition.Offset(mouseX, mouseY);
control.Location = nextPosition;
CtrlPos.x = nextPosition.X;
CtrlPos.y = nextPosition.Y;
Invalidate();
}
}
My code is the parent control (PictureBox) contains all the controls.
In my class I am using this:
private void picPreview_MouseMove(object sender, MouseEventArgs e)
{
if (SelectedControl != null && e.Button == MouseButtons.Left)
{
timer1.Stop();
Invalidate();
if (SelectedControl.Height < 20)
{
SelectedControl.Height = 20;
direction = Direction.None;
Cursor = Cursors.Default;
return;
}
else if (SelectedControl.Width < 20)
{
SelectedControl.Width = 20;
direction = Direction.None;
Cursor = Cursors.Default;
return;
}
//get the current mouse position relative the the app
Point pos = picPreview.PointToClient(MousePosition);
#region resize the control in 8 directions
if (direction == Direction.NW)
{
//north west, location and width, height change
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Size.Width - (newLocation.X - SelectedControl.Location.X),
SelectedControl.Size.Height - (newLocation.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
CtrlPos.x = newLocation.X;
CtrlPos.y = newLocation.Y;
SelectedControl.Size = newSize;
}
else if (direction == Direction.SE)
{
//south east, width and height change
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Size.Width + (newLocation.X - SelectedControl.Size.Width - SelectedControl.Location.X),
SelectedControl.Height + (newLocation.Y - SelectedControl.Height - SelectedControl.Location.Y));
SelectedControl.Size = newSize;
}
else if (direction == Direction.N)
{
//north, location and height change
newLocation = new Point(SelectedControl.Location.X, pos.Y);
newSize = new Size(SelectedControl.Width,
SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.S)
{
//south, only the height changes
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Width,
pos.Y - SelectedControl.Location.Y);
SelectedControl.Size = newSize;
}
else if (direction == Direction.W)
{
//west, location and width will change
newLocation = new Point(pos.X, SelectedControl.Location.Y);
newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
SelectedControl.Height);
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.E)
{
//east, only width changes
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(pos.X - SelectedControl.Location.X,
SelectedControl.Height);
SelectedControl.Size = newSize;
}
else if (direction == Direction.SW)
{
//south west, location, width and height change
newLocation = new Point(pos.X, SelectedControl.Location.Y);
newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
pos.Y - SelectedControl.Location.Y);
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.NE)
{
//north east, location, width and height change
newLocation = new Point(SelectedControl.Location.X, pos.Y);
newSize = new Size(pos.X - SelectedControl.Location.X,
SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
#endregion
}
}
Draw the image using CtrlPost (x, y):
g.DrawImage(
DrawText(image, new Font(cbxFont.Text, fontSize), colorInput,
Color.Transparent),
new Point(CtrlPos.x, CtrlPos.y));
// g is Graphics object.
Updated: I used the code to add label1
in pictureBox1
as follows:
pictureBox1.Controls.Add(label1);
source to share
If yours PictureBox
is at Sizemodes
StretchImage
or Zoom
, then the pixels are scaled; Label
Location
however, no. This way you would calculate the position where to draw:
PointF stretched(Point p0, PictureBox pb)
{
if (pb.Image == null) return PointF.Empty;
float scaleX = 1f * pb.Image.Width / pb.ClientSize.Width;
float scaleY = 1f * pb.Image.Height / pb.ClientSize.Height;
return new PointF(p0.X * scaleX, p0.Y * scaleY);
}
You would call him PointF p1 = stretched(p0, pictureBox1);
You could draw maybe like this:
g.DrawImage( DrawText(image, new Font(cbxFont.Text, fontSize),
colorInput, Color.Transparent),
Point.Round(stretched( CtrlPos.Location, picPreview));
If you also want to correct the size, you can use a similar function.
If SizeMode
- CenterImage
, pixels are not scaled, but most likely transposed and correction is required.
For the other direction, just switch the denominator and numerator in the fraction!
source to share