Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

picturebox image resize mouse wheel

Status
Not open for further replies.

lrfcbabe

Programmer
Jul 19, 2001
108
US
How do I resize a picturebox using the mouse wheel?
AKA zooming in and out using the wheel.
I can adjust the image to fit the screen if it is taller or wider than its bounding area.
PB1.height = pb1.height * 0.1 for mouse wheel back
PB1.height = pb1.height * 1.1 for mouse wheel forward
I can't seem to capture the wheel event to do this.
Thanks
 
It is there in 2005/2008, but it isn't shown in the designer you just have to add the sub with handles yourself.

Code:
Private Sub PictureBox1_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseWheel
End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I am almost there. There is still a sizing issue. From the initial zoom it appears to elongate the image and then it is distorted from then on. Also it is zooming from the upper left corner no from the cursor. This is what I have so far.

Private Sub PB1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PB1.MouseWheel
If PB1.Visible Then
PB1.SizeMode = PictureBoxSizeMode.StretchImage
If e.Button = 0 Then
'Windows.Forms.MouseButtons.Middle Then
'Debug.Print(e.Delta)
If e.Delta > 0 Then
PB1.Height = PB1.Height + PB1.Height * 1.1
PB1.Width = PB1.Width + PB1.Width * 1.1
Else
PB1.Height = PB1.Height - PB1.Height * 0.1
PB1.Width = PB1.Width - PB1.Width * 0.1
End If
End If
End If
End Sub
 
Your first problem is "PB1.SizeMode = PictureBoxSizeMode.StretchImage" which tells it to stretch the image. Next when you "zoom" in the picture all you are doing is changing the size of the picture box you are not zooming the image so to speak.

(0,0)
************************
************************
************************
************************
************************
Width = 24
Height = 5

Becomes

(0,0)
**************************
**************************
**************************
**************************
**************************
**************************
Width = 26
Height = 6

You haven't zoomed so to speak all you have done is made the picture box wider and taller. It becomes bigger from the location 0,0. What you want to do depends on what you want to do.

Lets go with you want to make the picture bigger/smaller and centered since that was the direction you were heading. This isn't necessarily the best way to do it.

This uses a PictureBox and a Panel. I assign the original picture to the tag so that we are always making drawing the picture from the original picture that isn't distorted from making it bigger/smaller.
Code:
    Dim ZoomFactor As Integer = 0
    Dim MinSize As Integer = 20
    Dim CurrentImage As Image

    Private Sub Start()
        'Some how got the picture and put it in CurrentImage
        'Check to make sure there is an image.
        If CurrentImage IsNot Nothing Then
            PictureBox2.Tag = CurrentImage

            'Set the picture.
            BigPB(PictureBox2)
        End If
    End Sub

    Private Sub PictureBox_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseWheel
        ZoomFactor = ZoomFactor + (e.Delta / 10)

        Panel1.AutoScroll = False 'AutoScroll will mess up actually placing the picture in the center of the panel
        BigPB(sender)
        Panel1.AutoScroll = True 'Now we can turn it back on.
    End Sub

    Private Sub BigPB(ByVal pb As PictureBox)
        Dim orgPic As Bitmap = pb.Tag

        'Calculate Width/Height
        Dim newPBWidth As Integer = Math.Max(orgPic.Width + ZoomFactor, MinSize)
        Dim newPBHeight As Integer = Math.Max(orgPic.Height + ZoomFactor, MinSize)

        'Calculate Center
        Dim PBnewCenter As New Point(newPBWidth / 2, newPBHeight / 2)
        Dim PNLmyCenter As New Point(Panel1.Width / 2, Panel1.Height / 2)

        'Calculate X/Y Location
        Dim PBx As Integer = PNLmyCenter.X - PBnewCenter.X
        Dim PBy As Integer = PNLmyCenter.Y - PBnewCenter.Y

        'Set PB
        pb.Size = New Size(newPBWidth, newPBHeight)
        pb.Location = New Point(PBx, PBy)

        Dim newImage As New Bitmap(orgPic, pb.Size)
        pb.Image = newImage

        xlbl.Text = pb.Location.X
        ylbl.Text = pb.Location.Y
        wlbl.Text = pb.Width
        hlbl.Text = pb.Height
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Well I created a new project and pasted what you had posted.
For the life of me I could not get the MouseWheel event to fire. I included the imports that I used in my original post with no luck. I worked on my original post and I have it working. This is what I came up with.

Private Sub PB1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PB1.MouseWheel

If PB1.Visible Then
If e.Button = 0 Then
If e.Delta > 0 Then
PB1.Height = PB1.Height + PB1.Height * 0.075
PB1.Width = PB1.Width + PB1.Width * 0.075
PB1.SizeMode = PictureBoxSizeMode.StretchImage
PB1.Left = PB1.Left - (e.X * 0.075)
PB1.Top = PB1.Top - (e.Y * 0.075)
Else
PB1.Height = PB1.Height - PB1.Height * 0.075
PB1.Width = PB1.Width - PB1.Width * 0.075
PB1.SizeMode = PictureBoxSizeMode.StretchImage
PB1.Left = (e.X * 0.075) + PB1.Left
PB1.Top = (e.Y * 0.075) + PB1.Top
End If
End If
End If

End Sub

Any idea why I could not get the event to work?
 
Did you change "PictureBox2.MouseWheel" or call your PictureBox "PictureBox2"? Other wise I'm not sure why the event didn't fire.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I added the form load event to get the picture to show on startup. Also I added the imports at the top, probably didn't need them all they were just what I had in my project.
PictureBox2 is spelled correctly. The image loads. I added PictureBox2.focus() in the Main Load event. This is what I have done to your original post.

Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Text
Imports System.Windows.Forms


Public Class Main
Dim ZoomFactor As Integer = 0
Dim MinSize As Integer = 20
Dim CurrentImage As Image

'Private Sub Start()
' 'Some how got the picture and put it in CurrentImage
' 'Check to make sure there is an image.
' CurrentImage = Image.FromFile("F:\1021 mark twain.jpg")
' If CurrentImage IsNot Nothing Then
' PictureBox2.Tag = CurrentImage
' 'Set the picture.
' BigPB(PictureBox2)
' End If
'End Sub

Private Sub PictureBox2_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseWheel
ZoomFactor = ZoomFactor + (e.Delta / 10)
Panel1.AutoScroll = False 'AutoScroll will mess up actually placing the picture in the center of the panel
BigPB(sender)
Panel1.AutoScroll = True 'Now we can turn it back on.
End Sub

Private Sub BigPB(ByVal pb As PictureBox)
Dim orgPic As Bitmap = pb.Tag
'Calculate Width/Height
Dim newPBWidth As Integer = Math.Max(orgPic.Width + ZoomFactor, MinSize)
Dim newPBHeight As Integer = Math.Max(orgPic.Height + ZoomFactor, MinSize)
'Calculate Center
Dim PBnewCenter As New Point(newPBWidth / 2, newPBHeight / 2)
Dim PNLmyCenter As New Point(Panel1.Width / 2, Panel1.Height / 2)
'Calculate X/Y Location
Dim PBx As Integer = PNLmyCenter.X - PBnewCenter.X
Dim PBy As Integer = PNLmyCenter.Y - PBnewCenter.Y
'Set PB
pb.Size = New Size(newPBWidth, newPBHeight)
pb.Location = New Point(PBx, PBy)
Dim newImage As New Bitmap(orgPic, pb.Size)
pb.Image = newImage
xlbl.Text = pb.Location.X.ToString
ylbl.Text = pb.Location.Y.ToString
wlbl.Text = pb.Width.ToString
hlbl.Text = pb.Height.ToString
End Sub

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

CurrentImage = Image.FromFile("F:\1021 mark twain.jpg")
If CurrentImage IsNot Nothing Then
PictureBox2.Tag = CurrentImage
'Set the picture.
BigPB(PictureBox2)
PictureBox2.Focus()
End If

End Sub
End Class
 
If the name is right I don't know what to tell you. The even should fire. You don't have another picture box over it or any thing? You could set the focus in the click event for PictureBox2 just to make sure it has the focus. You shouldn't need to do that but if something is some how stealing the focus that might help it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Well I have it working, if it isn't the best way to do it I will just have to settle for it. I took the app home and it did not even run, I get build errors. I loaded it up on a laptop and got the error but it would run from its last seccesful build. I tried clean build with no luck. I may try reinstalling VS08 maybe that will help.
Thanks
P.S. Where do I close this thread?
 
Ummm...The build errors would be an indication of what is wrong. If we took a look at them we might be able to fix it, but if you need to give up on it for now that is fine. You can't close a thread here if you just don't have the time or inclination to keep working on something then just mention you don't. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Reinstalling VS08 at home seemed to clear up my problems with the build errors. After a nasty virus took over my PC I deleted my profile and recreated it not thinking that alot of things would probably not work anymore. My PC runs so much cleaner now. I do have another question though and it is related to this same project. I want to make a call to the picturebox1_click event from a timer1_tick event. What I want to do is create a slideshow using a checkbox which enables the timer that calls the picturebox1 event which increments through an imagelist or listbox in my case. And I am wondering which arguments to use for the call? The sender would be Timer1, but what about the system.eventargs, what do I need there? Could the event handlers work better here?

XX
 
Generally it is better to move what you need for the time out of the click event to a common sub/function each can use. If you still want to do it with the click event then you would want to try a break on the click event and see what the event arguments contains. I don't remember off the top of my head, but likely it is mouse information. The sender is generally the object that calls the event or some portion of that object, so if you are having the program pretend the picture box has been clicked then you would send the picture box.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
THX for the brain shift Sorwen. I decided to use the listbox instead of the picbox. It works great. Now I need some sort of inputbox or scrollbar for the user to set the Timer Tick.

Private Sub CBSS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBSS.CheckedChanged

If CBSS.Checked = True Then
TV1.Visible = False
LB1.Visible = False
LB2.Visible = False
P1.Visible = False
CBSS.Visible = False
Xit.Visible = False
Timer1.Enabled = True
Else
TV1.Visible = True
LB1.Visible = True
LB2.Visible = True
P1.Visible = True
CBSS.Visible = True
Xit.Visible = True
End If

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If LB1.Items.Count > 0 Then
If LB1.SelectedIndex = -1 Or _
LB1.SelectedIndex = LB1.Items.Count - 1 Then
LB1.SelectedIndex = 0
Else
LB1.SelectedIndex = LB1.SelectedIndex + 1
End If
End If

End Sub

I hope everyone is getting this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top