Results 1 to 14 of 14

Thread: Showing a label on a picture

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    764

    Showing a label on a picture

    I have a picturebox and a label.
    The label is nested inside the picturebox.
    The picturebox at design time is square. When a picture is displayed in it, it is resized, and also turns into a rectangle that follows the same height/width ratio as the picture that it displays.
    All of that has been already achieved.

    But, now I want to display a picture in the picturebox and also display a short text in the label, and make the label appear in the center of the picture.
    And here lie some problems:
    When I code like this:
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
    The picture is shown in the picturebox, but the label is not shown.

    Please note that the Picturebox's Scalemode is 3-Pixel, so a conversion is not needed.
    But just I decided to give it a try. Who knows maybe I am missing something and it works. So I added two conversion lines to the end:
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
       
       lblFileNum.Top = CLng(lblFileNum.Top / Screen.TwipsPerPixelY)
       lblFileNum.Left = CLng(lblFileNum.Left / Screen.TwipsPerPixelX)
    Now, the label shows up on the picture, but it is not center-justified:
    https://i.imgur.com/806bxhI.jpg

    I check the values of Screen.TwipsPerPixelX and Screen.TwipsPerPixelY and both are 15
    I hardcode a larger number for example 16 as follows
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
       
       lblFileNum.Top = CLng(lblFileNum.Top / 16)
       lblFileNum.Left = CLng(lblFileNum.Left / 16)
    Nothing changes.
    I hardcode another slightly larger number 17
    Maybe a tiny difference but not much.

    After increasing that hard-coded value, the number 21 does the job and properly center-justifies the label:
    https://i.imgur.com/ouLSP6H.jpg

    This appears to have solved the problem, but, I am not sure if this is reliable or not.
    Also, I don't understand why it solves the problem especially noting that the Picturebox's Scalemode is 3-Pixel

    Can anybody please help with this?
    Thanks

  2. #2
    Junior Member Derp!'s Avatar
    Join Date
    Jan 2019
    Posts
    23

    Re: Showing a label on a picture

    Quote Originally Posted by IliaPreston View Post
    I have a picturebox and a label.
    The label is nested inside the picturebox.
    The picturebox at design time is square. When a picture is displayed in it, it is resized, and also turns into a rectangle that follows the same height/width ratio as the picture that it displays.
    All of that has been already achieved.

    But, now I want to display a picture in the picturebox and also display a short text in the label, and make the label appear in the center of the picture.
    And here lie some problems:
    When I code like this:
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
    The picture is shown in the picturebox, but the label is not shown.

    Please note that the Picturebox's Scalemode is 3-Pixel, so a conversion is not needed.
    But just I decided to give it a try. Who knows maybe I am missing something and it works. So I added two conversion lines to the end:
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
       
       lblFileNum.Top = CLng(lblFileNum.Top / Screen.TwipsPerPixelY)
       lblFileNum.Left = CLng(lblFileNum.Left / Screen.TwipsPerPixelX)
    Now, the label shows up on the picture, but it is not center-justified:
    https://i.imgur.com/806bxhI.jpg

    I check the values of Screen.TwipsPerPixelX and Screen.TwipsPerPixelY and both are 15
    I hardcode a larger number for example 16 as follows
    Code:
       Call Load_Picture(File_Path, Picture4, False)
       
       lblFileNum.Caption = "(" & FileNumber & ")"
       lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
       lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
       lblFileNum.ZOrder 0
       
       lblFileNum.Top = CLng(lblFileNum.Top / 16)
       lblFileNum.Left = CLng(lblFileNum.Left / 16)
    Nothing changes.
    I hardcode another slightly larger number 17
    Maybe a tiny difference but not much.

    After increasing that hard-coded value, the number 21 does the job and properly center-justifies the label:
    https://i.imgur.com/ouLSP6H.jpg

    This appears to have solved the problem, but, I am not sure if this is reliable or not.
    Also, I don't understand why it solves the problem especially noting that the Picturebox's Scalemode is 3-Pixel

    Can anybody please help with this?
    Thanks
    Change
    Call Load_Picture(File_Path, Picture4, False)

    lblFileNum.Caption = "(" & FileNumber & ")"
    lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
    lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
    lblFileNum.ZOrder 0

    to
    Call Load_Picture(File_Path, Picture4, False)

    lblFileNum.Caption = "(" & FileNumber & ")"
    lblFileNum.Top = (Picture4.ScaleHeight - lblFileNum.Height) \ 2
    lblFileNum.Left = (Picture4.ScaleWidth - lblFileNum.Width) \ 2
    lblFileNum.ZOrder 0


    If your form's scale mode isn't pixels then the height of the picture is given in twips (probably) and will offset your label off the visible part of the picture.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    764

    Re: Showing a label on a picture

    Quote Originally Posted by Derp! View Post
    Change
    Call Load_Picture(File_Path, Picture4, False)

    lblFileNum.Caption = "(" & FileNumber & ")"
    lblFileNum.Top = (Picture4.Height - lblFileNum.Height) \ 2
    lblFileNum.Left = (Picture4.Width - lblFileNum.Width) \ 2
    lblFileNum.ZOrder 0

    to
    Call Load_Picture(File_Path, Picture4, False)

    lblFileNum.Caption = "(" & FileNumber & ")"
    lblFileNum.Top = (Picture4.ScaleHeight - lblFileNum.Height) \ 2
    lblFileNum.Left = (Picture4.ScaleWidth - lblFileNum.Width) \ 2
    lblFileNum.ZOrder 0


    If your form's scale mode isn't pixels then the height of the picture is given in twips (probably) and will offset your label off the visible part of the picture.
    Yes, I used your recommended change and it actually solved the problem.
    But, I still don't understand why.

    What is the difference between the .Height and the .ScaleHeight properties of a picturebox?
    Same question about .Width and .scaleWidth

    Please advise.
    Thanks
    Ilia

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Showing a label on a picture

    Any window, picturebox is a window, has a non-client area and client area
    The non-client area includes the borders, titlebar, menubar, min/max/close buttons, etc
    The client area is where the content of the window is drawn and used to contain controls.

    ScaleWidth is the client are dimensions. FYI: API GetClientRect returns this
    Width is the entire window width including the non-client area. FYI: API GetWindowRect returns this

    In short, ScaleWidth is the size of the control, less the non-client area (borders, etc).
    Height & ScaleHeight are similar but relative to height vs width.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Showing a label on a picture

    Quote Originally Posted by IliaPreston View Post
    Yes, I used your recommended change and it actually solved the problem.
    But, I still don't understand why.

    What is the difference between the .Height and the .ScaleHeight properties of a picturebox?
    Same question about .Width and .scaleWidth

    Please advise.
    Thanks
    Ilia
    PictureBoxes are containers. Height and Width refer to the dimention of the control (the PictureBox) in its container. The values depend on the ScaleMode setting of the PictureBox's container.
    ScaleHeight and ScaleWidth are the dimentions, internal, of the PictureBox as a container, and they depend on the ScaleMode property setting of the PictureBox itself.

    For the controls contained (or whatever calculation) inside the PictureBox it should be used ScaleHeight and ScaleWidth, and for controls or whatever calculation outside the PictureBox, not for controls contained inside it, it should be used Height and Width.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Showing a label on a picture

    The awkward, broken, cartoony, obsolete SSTab control is an oddball and lacks any such client area dimension properties. But it was only built for use in fixed-size dialog forms so scaling at resize isn't such a problem there.

    The replacement TabStrip common control provides .ClientLeft, .ClientWidth, etc. so it's an outlier as well. But it least it has them. This is one of the reasons why it is a better fit in programs written after the 16-bit Windows era.

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Showing a label on a picture

    Quote Originally Posted by dilettante View Post
    The awkward, broken, cartoony, obsolete SSTab control is an oddball and lacks any such client area dimension properties. But it was only built for use in fixed-size dialog forms so scaling at resize isn't such a problem there.

    The replacement TabStrip common control provides .ClientLeft, .ClientWidth, etc. so it's an outlier as well. But it least it has them. This is one of the reasons why it is a better fit in programs written after the 16-bit Windows era.
    It is not only the SStab control that doesn't implement Scale properties, but also the Frame control. In fact, the only container control that implements Scale that I'm aware is the Picturebox. The UserControl has it but it is not exposed to the outside.
    (The TabStrip control isn't a control container)

    Then: Form, UserControl and PictureBox has Scale. Do you know of something else that implements it?

    And about the SSTab, if you dislike it, then you can try using my SSTab replacement, that does what SSTab does (without the bugs), and the TABStrip, and more.

    I still don't get why you hate the SSTab control so much, in some ways it is better than the TabStrip (and I'm talking about the original).

    But anyway, pehaps I have to thank you for ranting about the SSTab at any time that you can, because of your rantings (in part) I wrote the replacement. For me it has been useful, I don't know if someone else is using it, and I don't care.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Showing a label on a picture

    Quote Originally Posted by Eduardo- View Post
    ...
    Then: Form, UserControl and PictureBox has Scale. Do you know of something else that implements it?
    ...
    I believe the Printer object has Scale as well.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Showing a label on a picture

    Quote Originally Posted by passel View Post
    I believe the Printer object has Scale as well.
    Yes, you are right. I was thinking about screen objects that can be control containers, but yes.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Showing a label on a picture

    Quote Originally Posted by Eduardo- View Post
    Then: Form, UserControl and PictureBox has Scale. Do you know of something else that implements it?
    Well, the Frame control is also a container and ts scale mode is always twips, i.e., controls placed inside a frame have twips dimensions/positions.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Showing a label on a picture

    We were referring to the difference between Width, Height vs ScaleWidth and ScaleHeight.
    The Frame controls scale can't be changed, so doesn't have a SacleWidth or ScaleHeight property, i.e. it doesn't have a scale method nor scalemode property.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Showing a label on a picture

    Quote Originally Posted by LaVolpe View Post
    Well, the Frame control is also a container and ts scale mode is always twips, i.e., controls placed inside a frame have twips dimensions/positions.
    That what we were saying: that there are no more objects that have ScaleMode/ScaleTop/Left/Width/Height properties and Scale, ScaleX/Y methods.
    In fact, as a control, the only one is PictureBox.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Showing a label on a picture

    Instead of moving lblFileNum on Picture4 put the label inside of another picturebox of scalemode pixel (picMsgHolder, for example), make the label the same size as the picturebox, then center the picturebox on Picture4 doing away with this Screen.TwipsPerPixelX and Screen.TwipsPerPixelY stuff and all that other calculations you did and hard coded numbers

    Code:
      '
      '
     Dim strFileNum As String
     
     strFileNum = "(" & FileNumber & ")"
       
     picMsgHolder.Width = TextWidth(strFileNum) + 6  'add a small right margin if desired
     
     lblFileNum.Width = picMsgHolder.Width: lblFileNum.Height = picMsgHolder.Height
     lblFileNum.Caption = strFileNum
    
     picMsgHolder.Visible = True
    
     picMsgHolder.Move Picture4.Left + (Picture4.ScaleWidth / 2) - (picMsgHolder.ScaleWidth / 2), Picture4.Top + (Picture4.ScaleHeight / 2) - (picMsgHolder.ScaleHeight / 2)
      '
      '
    The above .Move is used if picMsgHolder is not a child of Picture4. If you want picMsgHolder to be a child of Picture4 then use below code, slightly simpler

    Code:
    picMsgHolder.Move (Picture4.ScaleWidth - picMsgHolder.ScaleWidth) / 2, (Picture4.ScaleHeight - picMsgHolder.ScaleHeight) / 2
    Attached Files Attached Files
    Last edited by Ordinary Guy; Oct 8th, 2019 at 09:19 PM.

  14. #14
    New Member
    Join Date
    Sep 2019
    Location
    Delhi
    Posts
    1

    Re: Showing a label on a picture

    yes this code is working..thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width