I want to create a map, with image of a floor plan. As the user zooms in or out, or moves the image around the form and clicks on a room to see more information about it, it will be possible. I'm thinking of creating labels or buttons with opasity set to 0%, over the rooms. However, when I zoomed in, the label stayed in the same location, it didn't move with the image as it was zoomed in/out, moved. How could this be solved in a painless way. Thanks for you efforts.

You will have to size the lables accordingly.

One idea would be to get the size ratio of the label and using that ratio scale the label when the image is scaled.

You will also have to move the label when the image is moved. One way you might be able to do this is to get the ammount the image has moved (X2 - X1,Y2 - Y1) Then add that to the current location of the label (X,Y).

Edited 3 Years Ago by Begginnerdev

OK. I found the code for the label to move with image, but I'm lost when I'm trying to create the code for label to enlarge/shrink when the image is zoomed in or out.

'The label is moved with the image as the image is being moved around the form.
Label1.Location = New Point(Label1.Location.X + e.X - myCoolPoint.X, Label1.Location.Y + e.Y - myCoolPoint.Y)

I've tried this code in order to make the label enlarge, but for some reason this doesn't work.

Private Sub Form_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel 
 _scaleDelta = Math.Sqrt(Label1.Width * Label1.Height) * 0.00001
        If e.Delta < 0 Then
            Label1.Size = New Size(Label1.Size.Width - 10, Label1.Size.Height - 10)
        ElseIf e.Delta > 0 Then
            Label1.Size = New Size(Label1.Size.Width + 10, Label1.Size.Height + 10)
        End If
End Sub

I was able to make the label increse the size, by setting AutoSize to False. One problem, is that the label increases the size too quick and the location of the label is messing up (not staying where it should).

Your code simply adds 10 (or subtracts) to the label no matter if the user scales the image 10 pixels.

You will have to get the amount the image changed and reflect that change on the label.

As for the label saying the wrong location, do you have code that changes this? Or is this bug due to resize?

Edited 3 Years Ago by Begginnerdev

Well, I don't know how "to get the amount the image changed and reflect that change on the label". And I also don't have the code to change the location of the label. Where could I find those codes OR could you give me examples?

Do you have the image inside a panel? Or maybe in PictureBox?

Something that has a Resize event can be used to compute change amount.

You could also look at using mousedown and mouseup as well. These would only change the label size when the user released the mouse button.

Here is an article that may help you with the logic.

I have the image placed inside of PictureBox, which is placed inside of a panel. How would I make the image changes reflect on the label though?

One thing you may actually want to look at is drawing on the image itself. Is there a specific reason you want a lable? Will you be chaning text on runtime?

If not, you can draw over the image with something like this:

Using g As Graphics = Graphics.FromImage(PictureBox1.BackgroundImage) ' or .Image
    g.DrawString("Hello World", New Font("Times New Roman", 12), Brushes.Black, New Point(0, 0))
End Using

This would move with the image and resize with the image.

This would cause a problem in the long run if you wanted to change the text on runtime.

One idea could be to draw a Rectangle to hold the text (maybe with a white background) and then write the text over it.

Private Sub DrawRect()
    Try
        Using g As Graphics = Graphics.FromImage(PictureBox1.BackgroundImage) ' or .Image

             g.FillRectangle(Brushes.White, New Rectangle(0, 0, 70, 20)) '70 x 20 is usual size of a textbox
             g.DrawString("Hello World", New Font("Times New Roman", 12), Brushes.Black, New Point(0, 0))
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Does this give you a good starting point?

Edited 3 Years Ago by Begginnerdev

I've tried to plug in the code into my exsisting program and in an empty one, but in neither one the code did not work. WHY?

This article has been dead for over six months. Start a new discussion instead.