The ratio of the coordinates of the PictureBox element

I ask the question here because I'm not sure how to search it in google, I don’t know what to call it.

I mean the "zero point", which is mainly located in the upper left corner of the element, pictureBox for example. So when I set the new width and height properties for my PictureBox, it works fine, but with respect to the upper left corner.

I mean, you have a pictureBox, say, 100 x 100, and you want to reduce it to 50 x 100, so that you get "empty space" under your box, not on top. PictureBox counts the properties from this zero point in the upper left corner.

And I need to change this point to a different angle.
Therefore, when I change my height, he counts, not down. Help me please.

I really hope you understand me.

Wow, thanks guys for your advice! I tested the binding property and Top + = 50; Now, does not solve my problem. Let me try to describe it differently. You have an image (100px) with 50px grass at the bottom and 50px sky at the top. So you have a picture with a height of 100. If you set pictureBox.height = 50; you will see only the sky. But I need to leave only grass. The main problem that I see here is that this zero point is in the upper left corner.

Here is an example:

Simple image splitted onto the two sides: earth and sky

Click1 Click Event:

private void button1_Click(object sender, EventArgs e) { pictureBox1.Height = 150; } 

The result that you get after clicking the button:

result of the button press

But I need one more result:

enter image description here

As I understand it, this is because the height property is counted in the upper left corner. Therefore, if I change it to the lower left or right corner, it will work as I need. Please help me change this moment ...

MSDN link: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode(v=vs.71).aspx

Valid values ​​for this property are taken from the PictureBoxSizeMode enumeration. By default, in PictureBoxSizeMode.Normal mode, the image is placed in the upper left corner of the PictureBox, and any part of the image that is too large for the PictureBox is cropped. Using the value of PictureBoxSizeMode.StretchImage will stretch the image according to the PictureBox.

+4
source share
1 answer

If you are not using a container that automatically manages the control layout, you will have to move it yourself. I assume that you are resizing the control in the code using a string, for example:

 myPictureBox.Height = 50; 

You will also need to move the management location by the same amount:

 myPictureBox.Top += 50; 

Summarizing, something like this should do the trick for changing the height:

 void ResizeHeightFromBottom(this Control c, int newHeight) { var oldBottom = c.Bottom; c.Height = newHeight; c.Top = oldBottom - newHeight; } 

I will leave the resizing and at the same time both an exercise for the reader and an indication of an arbitrary control point.

0
source

Source: https://habr.com/ru/post/1444117/


All Articles