Windows Develop Bookmark and Share   
 index > Windows Forms General > User resizing PictureBox at runtime
 

User resizing PictureBox at runtime

I want to allow my users to select the border of the PictureBox and do a dynamic drag/resize. Any resources on how I could accomplish this?
Keith Chapman  Monday, June 11, 2007 6:03 PM

I've been trhu this before. I couldnt find any automated solution, and so i made all by myself.

First you'll need to get MousePosition and change Cursor whenever the pointer is on the PictureBox edge. Then you'll need to draw a rect object to show what will be the new size of your pictureBox.

After that just set the Size property of the PictureBox and call invalidate so the PictureBox can redraw itself in the window again.

Remenber to set the ImageSize property on the PictureBox to ImageStrech to make things easier.

Was this helpful?

Bruno

Bruno Silveira  Monday, June 11, 2007 7:43 PM

I've been trhu this before. I couldnt find any automated solution, and so i made all by myself.

First you'll need to get MousePosition and change Cursor whenever the pointer is on the PictureBox edge. Then you'll need to draw a rect object to show what will be the new size of your pictureBox.

After that just set the Size property of the PictureBox and call invalidate so the PictureBox can redraw itself in the window again.

Remenber to set the ImageSize property on the PictureBox to ImageStrech to make things easier.

Was this helpful?

Bruno

Bruno Silveira  Monday, June 11, 2007 7:43 PM
That is actually great help, thank you! The only problem I am having is detecting when the mouse is on the edge of the picture box..
Keith Chapman  Wednesday, June 13, 2007 3:43 PM

Keith,

do the follow:

Retrieve the X and Y coord from PictureBox.Location.X and PictureBox.Location.Y. Retrieve the size of your PictureBox with PictureBox.Size.Width and PictureBox.Size.Height.

Capture the OnMouseMove event on your form. Within the event check for MousePosition.X. If its equal to the PictureBox.Location.X + PictureBox.Size.Width then you reach the right border of the pictureBox. Like this:

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

this.label3.Text = this.pictureBox1.Location.X.ToString() + "/" + this.pictureBox1.Location.Y.ToString();

this.label4.Text = this.pictureBox1.Size.Height.ToString() + "/" + this.pictureBox1.Size.Width.ToString();

}

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

this.label1.Text = e.X.ToString();

this.label2.Text = e.Y.ToString();

if (e.X == this.pictureBox1.Location.X + this.pictureBox1.Size.Width)

{

Cursor.Current = Cursors.SizeWE;

}

}

private void Form1_Resize(object sender, EventArgs e)

{

this.label3.Text = this.pictureBox1.Location.X.ToString() + "/" + this.pictureBox1.Location.Y.ToString();

}

private void pictureBox1_Resize(object sender, EventArgs e)

{

this.label4.Text = this.pictureBox1.Size.Height.ToString() + "/" + this.pictureBox1.Size.Width.ToString();

}

}

This is just an example to take the right edge of the Control. Apply the same mechanism to othr edges and then resize the PictureBox.

Hope this helps

Bruno Silveira  Thursday, June 14, 2007 2:35 PM

You can use google to search for other answers

Custom Search

More Threads

• PInvoke question
• String to Int -IformatProvider exception?
• Guru ? on Error msgs
• username other than sa in SQL Server 2000
• print preview problem its urgent
• OnBeforeCheckedChanged?
• Form changes its size by itself!
• popup window.
• adding a combo box to toolbar
• Image Text wrapping in RichTextBox control???