Solved

Label transparency

Posted on 2004-04-06
6
3,362 Views
Last Modified: 2010-07-27
In my programm I'm putting a lot of labels on a picturebox dynamically, in some of the cases these labels overlap each other
but I still want to see the underlying text, I tried to make the backcolor transparent but that doesn't work. Does anybody know a solution for this problem.
0
Comment
Question by:RonaldBiemans
6 Comments
 
LVL 11

Expert Comment

by:thunderchicken
ID: 10764831
Are you aligning them to the top and so forth?
0
 
LVL 25

Author Comment

by:RonaldBiemans
ID: 10764922
Thunderchicken I don't quite understand what you mean.

The Labels are placed based on X and Y coordinates of the underlying bitmap, this is the code

    Public Sub DrawXYPoint(ByVal x1 As Single, ByVal y1 As Single, ByVal Color As Color, ByVal Radius As Integer, ByVal Label As String)

        Dim x2 As Integer = CInt((x1 - XBottom) * Xrange()) + Indent
        Dim y2 As Integer = CInt(PicChart.Height - Indent - CInt((y1 - YBottom) * Yrange()))

        Dim x As Double
        Dim y As Double
        Dim xylabel As New Label
        PicChart.Controls.Add(xylabel)
        Try
            For x = -Radius To Radius
                For y = -Math.Sqrt(Radius ^ 2 - x ^ 2) To Math.Sqrt(Radius * Radius - x * x)
                    BM.SetPixel(CInt(x2 + x), CInt(y2 + y), Color)
                Next
            Next
            With xylabel
                .Top = y2 - Radius
                .Text = Label
                .AutoSize = True
                .ForeColor = Color
                .BackColor = Color.Transparent
                .Left = x2 + Radius
                .Visible = True
            End With

        Catch es As Exception
        End Try

    End Sub
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 10765051
The MakeTransparent method finds the pixels of a certain color in an
image and sets their alpha components to 0, making those pixels
transparent. (As a side effect, it also sets those pixels' other
components to 0 making the pixels black. This doesn't seem to be
documented so you probably shouldn't rely on this behavior remaining
unchanged in future versions of VB .NET).

This program makes a Bitmap containing a label image. It calls the
Bitmap's MakeTransparent method to make its red pixels transparent. It
displays the resulting label image so you can see what it looks like.
The pixels that were red are now transparent so the image's background
color shows through.

The program then makes Bitmap and Graphics objects representing the main
 
image. It uses the Graphics object's DrawImage method to copy the label
image onto the main image and displays the result.

    Private Sub btnBlend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlend.Click
        ' Use MakeTransparent to make red pixels transparent.
        Dim bm_label As New Bitmap(picLabel.Image)
        bm_label.MakeTransparent(Color.Red)

        ' Display the result.
        picLabel.Image = bm_label

        ' Copy the label onto the main picture.
        Dim bm_dog As New Bitmap(picDog.Image)
        Dim gr As Graphics = Graphics.FromImage(bm_dog)
        gr.DrawImage(bm_label, _
            (bm_dog.Width - bm_label.Width) \ 2, _
            bm_dog.Height - bm_label.Height)

        ' Display the result.
        picDog.Image = bm_dog
    End Sub
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 19

Expert Comment

by:Desp
ID: 10765143
Label1.BackColor = Color.FromArgb(0, Label1.BackColor)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10765199
I did a small test in VB.NET 2003:

      Dim labelTest As New Label

      labelTest.Location = New Point(Me.PictureBox1.Width \ 2, Me.PictureBox1.Height \ 2)
      labelTest.Text = "Test label"
      labelTest.BackColor = Color.Transparent

      Me.PictureBox1.Controls.Add(labelTest)

The label color was transparent, and I could see the bitmap underneath.  You might want to do this:

       Dim xylabel As New Label

       With xylabel
           .Top = y2 - Radius
           .Text = Label
           .AutoSize = True
           .ForeColor = Color
           .BackColor = Color.Transparent
           .Left = x2 + Radius
           .Visible = True
       End With

        PicChart.Controls.Add(xylabel)

Bob
0
 
LVL 25

Author Comment

by:RonaldBiemans
ID: 10765879
Sorry If I wasn't clear, the background is transparent to the bitmap but not to the other labels in other words if 2 labels overlap I can't see the label thats underneath (I can see the bitmap)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net/SQL prevent a table from being deleted ever 7 34
Memory Usage 2 49
ADO.NET ENTITY DATA MODEL 3 30
.NET tools for adding thread safety to a web app? 3 9
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now