Results 1 to 9 of 9

Thread: Showing text over an image

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Question Showing text over an image

    I have a Windows Form Application written using Visual Basic. I am trying to put an image onto the form, using a PictureBox control and then write some text over the image using a Label control. I have done some searching online to try to figure this out, but nothing is working.

    When I instantiate the PictureBox first, then the Label, the label does not appear. When I instantiate the Label first, it appears, but Label.BackColor = Color.Transparent is not doing what I expect. The two pictures shows this second case, with a white and a transparent BackColor.

    Here is a simplified version of the code, which appears in the Form_Load subroutine:
    Code:
    Dim pb1 As New PictureBox
    pb1.ImageLocation = "<jpg file>"
    pb1.Location = New Point(x , y)
    Me.Controls.Add(pb1)
    
    lbl1.Parent = pbCurCloud
    lbl1.Location = New Point((x1, y)
    Me.Controls.Add(lbl1)
    I have tried adding lbl1.Parent = pb1 and lbl1.BringToFront(), but those have not helped. What I would like is for the label background to be transparent so that which is seen is the image and the text. What do I need to change?
    Attached Images Attached Images   

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Showing text over an image

    See, now we get to the actual information that should have been in your other thread. Now we might understand why you were trying to set the Text of a Label in the Paint event handler. If you provide a FULL and CLEAR explanation of a problem then we can provide advice appropriate to that problem.

    You have two choices here. You can get rid of the Label and draw text on the PictureBox in the Paint event handler of the PictureBox. That's what you probably ought to do. Alternatively, you would need to make the Label a child control of the PictureBox.

    If you stick with the Label then you need to understand that WinForms doesn't support real transparency. It provides fake transparency by having a child draw its parent in its background. What means is that, if you have a "transparent" Label displayed in front of a PictureBox when both are children of the form, the Label will draw the form in its background rather than the PictureBox. If you want the Label to draw the PictureBox in its background then it has to be a child of the PictureBox. That can only be done in code, not in the designer. What you should do is position the PictureBox and Label exactly where you want them in the designer and then add the following code to the form's Load event handler:
    vb.net Code:
    1. Dim screenLocation = PointToScreen(Label1.Location)
    2.  
    3. Label1.Parent = PictureBox1
    4. Label1.Location = PictureBox1.PointToClient(screenLocation)
    That will keep the Label in the same position relative to the screen but put it inside the PictureBox rather than outside. Note that z-order is then irrelevant, because one control is the parent of the other rather than their being siblings.

    If you want to create the controls at run-time then you need to add the Label to the PictureBox's Controls collection, not the form's. The Location must then be relative to the Picturebox, not the form.

  3. #3
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: Showing text over an image

    That should be a sticky in tutorials.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Showing text over an image

    Thank you. Drawing the text in the Paint event handler of the PictureBox seems like the better approach. I have been doing some searching and I see how to add a Paint handler tot he form, but not to a specific PictureBox.

    For example, I found this:
    Code:
    AddHandler PictureBox_Paint, AddressOf Me.PictureBox_Paint
    but it seems that this would affect every picture box on the form. Please show me the line(s) of code to add the Paint event to PictureBox pb1.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Showing text over an image

    Firstly, that code is not valid. Secondly, if you change it to be valid then it will work as you want.

    If you had spent a bit more time researching AddHandler then you'd know that the statement follows this format:
    vb.net Code:
    1. AddHandler TheObject.TheEvent, AddressOf TheMethod
    If you want to handle the Paint event of pb1 then Paint is TheEvent and pb1 is TheObject.

    You probably don't need to use AddHandler at all though, because you probably don't need to create a control at run time. Is there a good reason that you're not just adding this PictureBox to the form in the designer? There may be but I'm not seeing evidence of it here. If you create the control at run time then you have no choice but to use AddHandler to attach an event handler but if you add the control in the designer then you can use WithEvents/Handles. In that case, you can use the Properties window or the Navigation Bar in the code window to generate and attach the event handler.

    On the subject of using the Paint event for GDI+ drawing, you should include the code to draw the text in the Paint event handler and then, when you want to change the text, call the Invalidate method of the PictureBox and specify the smallest area you can that incorporates the text. You don't have to specify an area but then the whole area will get repainted. That is not necessarily a problem but can lead to flickering, as the repainting process is relatively slow.

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Showing text over an image

    I prefer to use graphics , bitmaps and rectangles instead of relying on paint event, so I can control other factors as the blending and quality but anyhow, admittedly it's way harder, so , I think this will help you.
    https://social.msdn.microsoft.com/Fo...-e22437287608/
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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

    Re: Showing text over an image

    Quote Originally Posted by jmcilhinney View Post
    ... You don't have to specify an area but then the whole area will get repainted. That is not necessarily a problem but can lead to flickering, as the repainting process is relatively slow.
    Since Pictureboxes are always double buffered by default, you shouldn't get flickering even when doing a full repaint, as long as you do repaint everything.
    "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

  8. #8
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Showing text over an image

    to prevent the flickering, you usually put also the doublebuffered property of the form to yes
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Showing text over an image

    Quote Originally Posted by jmcilhinney View Post
    That is not necessarily a problem but can lead to flickering, as the repainting process is relatively slow.
    That was an over-simplification but the fact is that you should not just lazily invalidate an entire control when you know that only a small part of it could have changed.

Tags for this Thread

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