C# 如何控制标签或任何控制图片盒的位置?

C# 如何控制标签或任何控制图片盒的位置?,c#,C#,我正在PictureBox中成功移动控件(标签或图像)。当我移动时,它将保存控制位置(x,y) 像这样: 但问题是:图像结果是: 在gif图像出现之前,我在中央屏幕上拖放了一个标签控件。但结果图像不会将标签保存在中央屏幕中 我已将PictureBox属性设置为StretchImage 在PictureBox中获取位置和绘图文本的代码,如: public PositionControl CtrlPos = new PositionControl(); private void control_

我正在PictureBox中成功移动控件(标签或图像)。当我移动时,它将保存控制位置(x,y)

像这样:

但问题是:图像结果是:

在gif图像出现之前,我在中央屏幕上拖放了一个标签控件。但结果图像不会将标签保存在中央屏幕中

我已将PictureBox属性设置为StretchImage

在PictureBox中获取位置和绘图文本的代码,如:

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();
    }
}
我的代码是父控件(PictureBox)包含所有控件

在我的课堂上,我用的是:

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
    }
}
更新: 我使用代码将
label1
添加到
pictureBox1
中,如下所示:

g.DrawImage(  DrawText(image, new Font(cbxFont.Text, fontSize), 
                       colorInput, Color.Transparent),
              Point.Round(stretched( CtrlPos.Location, picPreview));

pictureBox1.Controls.Add(label1)

如果您的
图片盒
位于
大小模式
拉伸图像
缩放
中,则像素将被缩放;但是,
标签
位置
不是。因此,您可以计算绘制位置:

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);
}
您可以将其称为
pointfp1=stretched(p0,pictureBox1)

你可能会这样画:

g.DrawImage(  DrawText(image, new Font(cbxFont.Text, fontSize), 
                       colorInput, Color.Transparent),
              Point.Round(stretched( CtrlPos.Location, picPreview));
如果您还想校正尺寸,可以使用类似的功能

如果
SizeMode
CenterImage
,则像素不会缩放,但很可能会进行转置,也需要进行校正


对于另一个方向,只需切换分数中的分母和分子

如果您的
图片盒
位于
Sizemodes
StretchImage
缩放
中,则会缩放像素;但是,
标签
位置
不是。因此,您可以计算绘制位置:

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);
}
您可以将其称为
pointfp1=stretched(p0,pictureBox1)

你可能会这样画:

g.DrawImage(  DrawText(image, new Font(cbxFont.Text, fontSize), 
                       colorInput, Color.Transparent),
              Point.Round(stretched( CtrlPos.Location, picPreview));
如果您还想校正尺寸,可以使用类似的功能

如果
SizeMode
CenterImage
,则像素不会缩放,但很可能会进行转置,也需要进行校正


对于另一个方向,只需切换分数中的分母和分子

尽管你做出了努力,我还是觉得这个问题不太清楚。首先想到的是:标签是放在图片盒上还是在里面?看见对您想要实现的目标进行更清晰的顶层描述也会有所帮助..我使用此代码将label1添加到pictureBox1。像
pictureBox1.Controls.Add(label1)。我只想将PictureBox设置为图像。当我将标签设置在位置:结果图像中PictureBox的中心或左侧时,将导出图像。目前,当我在PictureBox中设置位置时,结果中没有显示Point()。尽管您做出了努力,但我发现这个问题相当不清楚。首先想到的是:标签是放在图片盒上还是在里面?看见对您想要实现的目标进行更清晰的顶层描述也会有所帮助..我使用此代码将label1添加到pictureBox1。像
pictureBox1.Controls.Add(label1)。我只想将PictureBox设置为图像。当我将标签设置在位置:结果图像中PictureBox的中心或左侧时,将导出图像。当前,当我在PictureBox中设置位置时,结果中没有显示Point()。有图像吗?scalX/Y值可信吗?啊,这是另一个方向;有时你需要一个,有时需要另一个。我会更新答案..有图像了吗?scalX/Y值可信吗?啊,这是另一个方向;有时你需要一个,有时需要另一个。我会更新答案。。