How to control position of label or any control PictureBox?

前端 未结 1 1367
鱼传尺愫
鱼传尺愫 2020-12-07 04:42

I\'m moving control(label or image) success in PictureBox. When I move, it will save control position(x, y).

Like this:

But problem is: the image re

相关标签:
1条回答
  • 2020-12-07 05:27

    If your PictureBox is in Sizemodes StretchImage or Zoom then the pixels are scaled; the Label's Location however is not. So you would have the 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 it as PointF p1 = stretched(p0, pictureBox1);

    You would 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 the SizeMode is CenterImage the pixels are not scaled but most likely transposed and a correction is necessary as well.

    For the other direction simply switch denominator and numerator in the fractions!

    0 讨论(0)
提交回复
热议问题