Mike
Mike

Reputation: 1999

PictureBox size coordinates relation

I'm asking a question here cause I'm not sure how to search it in google, I don't know the word how to call this.

What I mean is a "zero point" which basically located at upper-left corner of the element, pictureBox for an example. So when I set new width and height properties for my pictureBox it works fine but it's relative to top left corner.

What I mean is you've got pictureBox, let's say 100 x 100 and you desire decrease it to 50 x 100, so you will get "empty space" under your picture box, not upper. PictureBox counts it's properties from this one top left corner zero point.

And what I need is to change this point to the another corner.
So when I change my height it count up, not down. Help me please.

I really hope you can understand me.

Wow, thank you guys for your advices! I've tested anchor property and Top += 50; now, doesn't solve my problem. Let me try to describe it another way. You have an image (100px) with grass 50px at bottom and sky 50px at the top. So you have picturebox with height 100. If you set pictureBox.height = 50; you will see only sky. But I need to leave only grass. The main problem I see here is because this zero point at top left corner.

Here is an example:

Simple image splitted onto the two sides: earth and sky

Button1 click event:

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

The result which you will get after you press the button:

result of the button press

But I need another result:

enter image description here

As I understand it happens because height property counts from the top left corner. So if I change it to bottom left or right corner it will works the way I need. Help me please to change this point...

MSDN reference: 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 too big for the PictureBox is clipped. Using the PictureBoxSizeMode.StretchImage value causes the image to stretch to fit the PictureBox.

Upvotes: 4

Views: 2425

Answers (1)

lc.
lc.

Reputation: 116508

Unless you are using a container which handles control layout automatically, you will have to move it yourself. I assume you are resizing the control in code with a line like:

myPictureBox.Height = 50;

You will have to also move the control's location down by the same amount:

myPictureBox.Top += 50;

Generalizing, something like this ought to do the trick for resizing height:

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

I will leave resizing the width and both simultaneously as an exercise for the reader, as well as specifying an arbitrary reference point.

Upvotes: 0

Related Questions