Functionality to resize pictures in runtime

nicki_bee

Member
Joined
Apr 4, 2005
Messages
8
Location
Chelmsford
Programming Experience
1-3
Hi I am trying to create an app that let's a user import a picture and then manipulate it. I would like them to be able to resize the picture in a similar way to how apps like word do, i.e. using handles round the edge of the images. Can anyone advise me where I would start with this sort functionality or if there are any tutorials on similar topics that could help me?

Many Thanks
 
Use an imagebox, and set the image to expand to fit. Then just size that. Sice it's expand to fit the image will auto size with it.

TPM
 
he means the picturebox

in vb6 there used to be two seperate controls (picturebox and imagebox) but when MS was developing all the .net stuff they merged the imagebox and picturebox into one

of which in .net it's the picturebox control
 
Thanks for that. I have tried using a picturebox on my form with the sizemode set to strechimage, however I would like to be able to resize the picture during runtime. I have found some code in vb6 which allows me to do this however i can not seem to get it to work in vb.Net. this is what i have so far.

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] components [/size][size=2][color=#0000ff]As[/color][/size][size=2] Container 
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Declare[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] GetWindowLong [/size][size=2][color=#0000ff]Lib[/color][/size][size=2] "user32" [/size][size=2][color=#0000ff]Alias[/color][/size][size=2] "GetWindowLongA" ([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hwnd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] nIndex [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size]
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Declare[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] SetWindowLong [/size][size=2][color=#0000ff]Lib[/color][/size][size=2] "user32" [/size][size=2][color=#0000ff]Alias[/color][/size][size=2] "SetWindowLongA" ([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hwnd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] nIndex [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] dwNewLong [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size]
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Declare[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] SetWindowPos [/size][size=2][color=#0000ff]Lib[/color][/size][size=2] "user32" ([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hwnd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hWndInsertAfter [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] x [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] y [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] cx [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] cy [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] wFlags [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Long[/color][/size]
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] WS_THICKFRAME = &H40000
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] GWL_STYLE = (-16)
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] SWP_NOZORDER = &H4
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] SWP_NOSIZE = &H1
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] SWP_NOMOVE = &H2
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] SWP_FRAMECHANGED = &H20 [/size][size=2][color=#008000]' The frame changed: send WM_NCCALCSIZE[/color][/size]
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] SWP_DRAWFRAME = SWP_FRAMECHANGED
 [/size]
 


Private Sub windows_forms_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load[indent]Dim window_style As Long   window_style = GetWindowLong(PictureBox2.Handle.ToInt32, WL_STYLE)
window_style = window_style Or WS_THICKFRAME
SetWindowLong(PictureBox2.Handle.ToInt32, GWL_STYLE, window_style)
SetWindowPos(PictureBox2.Handle.ToInt32, Me.Handle.ToInt32, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME)
[/indent] End Sub

Basically when i run it the image is no longer on the form. If i comment out the SetWindowPos line it does appear on the form however does not have the functionality to be re-sized. So i'm guessing it's something to do with the handles i'm passing to this function perhaps? Does anyone have any ideas?

Thank you
 
Hi,

You can try the following code to resize images. Typically you can resize images using the constructor of the Bitmap class which take the width and height as params.
If you multiply these params by a value between 0.1 to 0.9 then you will be able to reduce the size of the image.

img = New Bitmap(img, New Size(img.Size.Width * factor,
img.Size.Height * factor))

And then you can simple chose to save the image to another file or folder.

Let me know if that helps!
 
Thanks sdatye. So to use this function you would need the user to enter in a resizing percentage right?

I was hoping to find something that would allow the user to be able to click and drag the sides on the image like you would a text box in MSWord. Maybe it's not possible to do in VB.Net?

If I cannot find another way I will try with the Bitmap class - thanks
 
Hi nicki,

It is absolutely doable in VB.NET :) In the example I gave, I had hardcoded the resizing percentage. But in reality when you capture the mouse down event, and then when you start a drag, you will typically get the mousemove event as well, where-in you will be able to get the x and y coordinates. You will have to map the mouse's xand y coordinates, in relation to the image placement coordinates, and then simply pass the same to the Bitmap class constructor. :) I will try to post a sample code by EOD tomorrow!
 
I am trying to use the mouse events to reszie my image. At the moment I am using form events liek form1_MouseMove, but is there any way that I can get it to only resize when the user click and drags the edge of the image?

thanks
 
Sorry, I have worked out how to do this now, however I have another problem. I can now load an image onto the form and resize it, however i cannot get the image to stretch to fit the picture box. here is my code


VB.NET:
[size=2][color=#008000]' Handler for the ImageFileOpen command
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] onImageOpen([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] openImagefile.Click
[/size]
[size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] (fileLoadArea [/size][size=2][color=#0000ff]Is[/color][/size][size=2][color=#0000ff]Nothing[/color][/size][size=2]) [/size][size=2][color=#0000ff]Then
[/color][/size][size=2]	 fileLoadArea.Hide()
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2]mainPanel.Show()
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ofd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] OpenFileDialog()
ofd.Filter = "Image Files (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|" + "	All Files (*.*)|*.*"
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] ofd.ShowDialog() = DialogResult.OK [/size][size=2][color=#0000ff]Then
	 [/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] fileName [/size][size=2][color=#0000ff]As[/color][/size][size=2] [String] = ofd.FileName
	 [/size][size=2][color=#0000ff]If[/color][/size][size=2] fileName.Length <> 0 [/size][size=2][color=#0000ff]Then
		 [/color][/size][size=2][color=#0000ff]Try
			 [/color][/size][size=2]pictureBox1.BackgroundImage = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Bitmap(fileName)
			 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
			 pictureBox1.Show()
		 [/size][size=2][color=#0000ff]Catch
		 [/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
	 [/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#008000]'onImageOpen
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] _mouseDown [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean[/color][/size][size=2] = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] pictureBox1_MouseMove([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], _ 
[/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] pictureBox1.MouseMove
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] _mouseDown = [/size][size=2][color=#0000ff]True [/color][/size][size=2][color=#0000ff]Then
	 [/color][/size][size=2]pictureBox1.Width = e.X
	 pictureBox1.Height = e.Y
	 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
	 pictureBox1.Refresh()
	[/size][size=2][color=#0000ff]Else
[/color][/size][size=2][color=#0000ff]	If[/color][/size][size=2] e.X >= pictureBox1.Width - 3 [/size][size=2][color=#0000ff]And[/color][/size][size=2] e.Y >= pictureBox1.Height - 3 [/size][size=2][color=#0000ff]Then
		 [/color][/size][size=2]pictureBox1.Cursor = Cursors.SizeNWSE
	 [/size][size=2][color=#0000ff]ElseIf[/color][/size][size=2] e.X >= pictureBox1.Width - 3 [/size][size=2][color=#0000ff]Then
		 [/color][/size][size=2]pictureBox1.Cursor = Cursors.SizeWE
	 [/size][size=2][color=#0000ff]ElseIf[/color][/size][size=2] e.Y >= pictureBox1.Height - 3 [/size][size=2][color=#0000ff]Then
		 [/color][/size][size=2]pictureBox1.Cursor = Cursors.SizeNS
	 [/size][size=2][color=#0000ff]Else
		 [/color][/size][size=2]pictureBox1.Cursor = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].Cursor
	 [/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
	[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
 
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] pictureBox1_MouseDown([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], _[/size]
[size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] pictureBox1.MouseDown
 
_mouseDown = [/size][size=2][color=#0000ff]True
[/color][/size][size=2][color=#0000ff]If [/color][/size][size=2]e.X >= pictureBox1.Width - 3 [/size][size=2][color=#0000ff]And[/color][/size][size=2] e.Y >= pictureBox1.Height - 3 [/size][size=2][color=#0000ff]Then
[/color][/size][size=2]	pictureBox1.Cursor = Cursors.SizeNWSE
[/size][size=2][color=#0000ff]ElseIf[/color][/size][size=2] e.X >= pictureBox1.Width - 3 [/size][size=2][color=#0000ff]Then
	[/color][/size][size=2]pictureBox1.Cursor = Cursors.SizeWE
[/size][size=2][color=#0000ff]ElseIf[/color][/size][size=2] e.Y >= pictureBox1.Height - 3 [/size][size=2][color=#0000ff]Then
	[/color][/size][size=2]pictureBox1.Cursor = Cursors.SizeNS
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] pictureBox1_MouseUp([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], _
[/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] pictureBox1.MouseUp
 
_mouseDown = [/size][size=2][color=#0000ff]False
	[/color][/size][size=2]pictureBox1.Cursor = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].Cursor
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

I can't work out why the image will not stretch or shrink to the picturebox. Am I missing something?

thanks
 
Back
Top