C# put the picture in the specified position on the picturebox, interpreted by PointToClient and PointToScreen

1. How to put the picture in the specified position on the picturebox in C#

Construct a Bitmap with the same size as picturebox1,
set it to picturebox1,
and then draw on it

Bitmap image = new Bitmap(picturebox1.Size.Width, picturebox1.Size.Height);
Graphics device = Graphics.FromImage(image);
//如果picturebox1本身有内容,就先画到image上
device.DrawImage(picturebox2.Image, w, h); //用你想要的位置画picturebox2
picturebox1.Image = image;

2. PointToClient and PointToScreen

//
// Summary:
// Calculate the position of the specified screen point into the coordinates of the client area.
//
// Parameters:
// p:
// The screen coordinate System.Drawing.Point to convert.
//
// Return result:
// A System.Drawing.Point, which represents the converted System.Drawing.Point, p (in workspace coordinates).
public Point PointToClient(Point p);
//
// Summary:
// Calculate the position of the specified workspace point into screen coordinates.
//
// Parameters:
// p:
// The coordinates of the work area to be converted System.Drawing.Point.
//
// Return result:
// A System.Drawing.Point, which represents the converted System.Drawing.Point, p (expressed in screen coordinates).
public Point PointToScreen(Point p);

PointToClient compares the coordinates of the Screen (pt) with the coordinates of the Control on the Screen, and returns the difference. The coordinate center is the absolute position of the Control's Location on the Screen.

PointToScreen is to compare the coordinate point (pt) with the coordinate of Control on the Screen to return the superimposed value. The coordinate center is the upper left corner of the screen, which is (0, 0).

Quoting other explanations as follows:
insert image description here

PointToClient calculates the position of the specified screen point into the coordinates of the work area. The coordinate origin is the upper left corner of the yellow area, the X-axis direction is from left to right, and the Y-axis direction is from top to bottom, and the coordinate unit is pixel;
PointToScreen calculates the position of the specified workspace point as screen coordinates. The coordinate origin is at the upper left corner of the screen, the X-axis direction is from left to right, and the Y-axis direction is from top to bottom. The coordinate unit is pixel.

Write the demo as shown in the figure below, and describe it in detail.

insert image description here
1、PointToClient(Point p)

Here p, the coordinate origin is the upper left corner of the Screen, ie (0, 0).

It calculates the position of the current Control at the Screen position as the coordinate origin, and returns the value of p relative to the coordinate origin.

As shown in the above illustration, the position of btn in the screen is (328, 188), then the point (10, 10) on the screen relative to (320, 188) is the result (-318, -178), and the point (0 , 0) is (-183, -185) relative to pnl.

The calculation method of Control on the Screen is: the Location of the form plus the Location of its Parent at all levels.

2、PointToScreen(Point p)

Here p, the coordinate origin is relative to the Control's Location.

This is the superposition result of the Locaton of Control on the Screen.

As in the above example, the position of pnl relative to the form is (80, 60), which is added to the form Location (200, 100), plus the width of the border 3, the height of the title bar 25, which is 185.

btn.PointToScreen(new Point(30, 20)): {X=358,Y=208}

This value is the result of adding (30, 20) to the position (328, 188) of btn on the Screen.

3. Get the position of the control in the form

There is in Delphi, but not in c#, you need to write it yourself, and encapsulate a static function to realize it:

public static class ControlHelper
    {
    
    
        public static Point LocationOnClient(this Control c)
        {
    
    
            var retval = new Point(0, 0);
            for (; c.Parent != null; c = c.Parent)
                retval.Offset(c.Location);
            return retval;
        }
    }

Find the position of btn in the form:

lbl.Text = btn.LocationOnClient().ToString();    //{X=125, Y=63}

Guess you like

Origin blog.csdn.net/m0_65636467/article/details/129151459