-
Dec 17th, 2017, 11:14 AM
#1
Thread Starter
New Member
label1 not moving in position when zoom in, inside picturebox vb.net
hi guys please help me
I have a label1 inside in picture box and panel when zoom in label1 move in position. I want to stay the label1 in position on designated position I choose in the picture box how can I do that?
-
Dec 17th, 2017, 03:17 PM
#2
Re: label1 not moving in position when zoom in, inside picturebox vb.net
You could start by showing your code.
I have no idea how you are zooming in and positioning the label.
If you're increasing the size of the picturebox to zoom in, then probably you would need to move the label to keep it location relative by %.
-
Dec 17th, 2017, 06:59 PM
#3
Re: label1 not moving in position when zoom in, inside picturebox vb.net
The Location of a Label control won't change on its own. If it's changing then it's because you're changing it. Most likely what is actually happening is that the Location is not changing but you actually do want it to, so that it stays in the same position relative to something else. All we can do is guess though, because you've provided next to no information. Please ALWAYS provide a FULL and CLEAR description of the problem, which includes exactly what you're trying to achieve, exactly how you're trying to achieve it (including relevant code) and exactly what happens when you try and how that differs from your expectations (including relevant error messages and locations). We only know what you tell us so you have to tell us everything that's relevant.
-
Dec 18th, 2017, 02:46 AM
#4
Re: label1 not moving in position when zoom in, inside picturebox vb.net
Hi Asner,
Welcome to the forum. You can't just drop a Label into a PictureBox in the Designer because a PictureBox isn't a Container Control - unlike say a Panel. But it's easy to do it in code: just make the picture box the Parent of the label. If you have already added the label in the Designer, its Location will now be wrong. In that case set the Left and Top properties relative to the parent coordinates, for example:
Code:
Label1.Parent = PictureBox1
Label1.Left = 30
Label1.Top = 25
to set the Label1 at location 30,25 inside the picturebox.
BB
Last edited by boops boops; Dec 18th, 2017 at 02:59 AM.
-
Dec 18th, 2017, 04:33 AM
#5
Re: label1 not moving in position when zoom in, inside picturebox vb.net
 Originally Posted by boops boops
Hi Asner,
Welcome to the forum. You can't just drop a Label into a PictureBox in the Designer because a PictureBox isn't a Container Control - unlike say a Panel. But it's easy to do it in code: just make the picture box the Parent of the label. If you have already added the label in the Designer, its Location will now be wrong. In that case set the Left and Top properties relative to the parent coordinates, for example:
Code:
Label1.Parent = PictureBox1
Label1.Left = 30
Label1.Top = 25
to set the Label1 at location 30,25 inside the picturebox.
BB
In order to allow you to position the Label where you want it at design time and then simply change the Parent at run time, you can do this:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Location = PictureBox1.PointToClient(Me.PointToScreen(Label1.Location)) Label1.Parent = PictureBox1 End Sub
Note that you can also edit the designer code file by hand to make the PictureBox the Parent of the Label. You would change this:
vb.net Code:
Me.Controls.Add(Me.Label1)
to this:
vb.net Code:
Me.PictureBox1.Controls.Add(Me.Label1)
The designer will not overwrite it when you make further changes because it is perfectly valid code.
-
Dec 18th, 2017, 08:47 AM
#6
Thread Starter
New Member
Re: label1 not moving in position when zoom in, inside picturebox vb.net
 Originally Posted by jmcilhinney
In order to allow you to position the Label where you want it at design time and then simply change the Parent at run time, you can do this:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Location = PictureBox1.PointToClient(Me.PointToScreen(Label1.Location)) Label1.Parent = PictureBox1 End Sub
Note that you can also edit the designer code file by hand to make the PictureBox the Parent of the Label. You would change this:
vb.net Code:
Me.Controls.Add(Me.Label1)
to this:
vb.net Code:
Me.PictureBox1.Controls.Add(Me.Label1)
The designer will not overwrite it when you make further changes because it is perfectly valid code.
here sir this is my test form codes
Public Class testform
Dim CurPicture As Integer = 1
Private m_PanStartPoint As New Point
Private _originalSize As Size = Nothing
Private _scale As Single = 1
Private _scaleDelta As Single = 0.0005
Private Sub testform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Location = PictureBox1.PointToClient(Me.PointToScreen(Label1.Location))
Label1.Parent = PictureBox1
'Panel Settings
Panel1.AutoScroll = True
'Picture Box Settings
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'init this from here or a method depending on your needs
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Size = Panel1.Size
_originalSize = Panel1.Size
End If
Private Sub testform_MouseWheel(sender As Object, e As MouseEventArgs) Handles Me.MouseWheel
_scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
If e.Delta < 0 Then
_scale -= _scaleDelta
ElseIf e.Delta > 0 Then
_scale += _scaleDelta
End If
If e.Delta <> 0 Then _
PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)), _
CInt(Math.Round(_originalSize.Height * _scale)))
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
'Here we get the change in coordinates.
Dim DeltaX As Integer = (m_PanStartPoint.X - e.X)
Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y)
'Then we set the new autoscroll position.
'ALWAYS pass positive integers to the panels autoScrollPosition method
Panel1.AutoScrollPosition = _
New Drawing.Point((DeltaX - Panel1.AutoScrollPosition.X), _
(DeltaY - Panel1.AutoScrollPosition.Y))
End If
Last edited by asner17; Dec 18th, 2017 at 12:04 PM.
-
Dec 18th, 2017, 11:30 AM
#7
Re: label1 not moving in position when zoom in, inside picturebox vb.net
So it is along the lines that I guessed, i.e. you change the size of the picturebox to implement zooming (the image is auto Stretched to fit).
I don't see any mention of a label in the code.
As stated earlier, if you have added a label at some point within the picture then you'll need to keep track of that position (unscaled).
If you resize the picturebox by some scalefactor then you will need to scale the original location of the label by the same value so that its X,Y location remains relative to the pixel it was on unscaled.
Of course there are better ways to do what you are attempting, most likely, such as leaving the image size alone and drawing the image in the paint event at a zoomed scale. You could draw the text on the image in a separate operation, rather than align label controls in the picturebox. Perhaps you want the text to grow with the image, perhaps not.
Last edited by passel; Dec 18th, 2017 at 11:35 AM.
-
Dec 18th, 2017, 05:01 PM
#8
Re: label1 not moving in position when zoom in, inside picturebox vb.net
As you can see, boops and I both formatted our code for readabi8lity. Please do us the same courtesy.
vb.net Code:
Public Class testform Dim CurPicture As Integer = 1 Private m_PanStartPoint As New Point Private _originalSize As Size = Nothing Private _scale As Single = 1 Private _scaleDelta As Single = 0.0005 Private Sub testform_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Location = PictureBox1.PointToClient(Me.PointToScreen(Label1.Location)) Label1.Parent = PictureBox1 'Panel Settings Panel1.AutoScroll = True 'Picture Box Settings PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 'init this from here or a method depending on your needs If PictureBox1.Image IsNot Nothing Then PictureBox1.Size = Panel1.Size _originalSize = Panel1.Size End If Private Sub testform_MouseWheel(sender As Object, e As MouseEventArgs) Handles Me.MouseWheel _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005 If e.Delta < 0 Then _scale -= _scaleDelta ElseIf e.Delta > 0 Then _scale += _scaleDelta End If If e.Delta <> 0 Then _ PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)), _ CInt(Math.Round(_originalSize.Height * _scale))) Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then 'Here we get the change in coordinates. Dim DeltaX As Integer = (m_PanStartPoint.X - e.X) Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y) 'Then we set the new autoscroll position. 'ALWAYS pass positive integers to the panels autoScrollPosition method Panel1.AutoScrollPosition = _ New Drawing.Point((DeltaX - Panel1.AutoScrollPosition.X), _ (DeltaY - Panel1.AutoScrollPosition.Y)) End If
Also, why did you quote my most recent post when your post has nothing to do with that? The point of quoting is so that we know exactly what your post is referring to but your reply had nothing to do with what you quoted.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|