-
Jun 29th, 2022, 10:37 PM
#1
Thread Starter
Lively Member
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?
-
Jun 29th, 2022, 11:17 PM
#2
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:
Dim screenLocation = PointToScreen(Label1.Location)
Label1.Parent = PictureBox1
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.
-
Jun 30th, 2022, 06:07 AM
#3
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.
-
Jun 30th, 2022, 10:10 PM
#4
Thread Starter
Lively Member
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.
-
Jun 30th, 2022, 11:00 PM
#5
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:
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.
-
Jul 1st, 2022, 09:37 AM
#6
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/
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2022, 11:09 AM
#7
Re: Showing text over an image
Originally Posted by jmcilhinney
... 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
-
Jul 2nd, 2022, 04:19 AM
#8
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)
-
Jul 2nd, 2022, 06:39 AM
#9
Re: Showing text over an image
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|