Solved

Label transparency

Posted on 2004-04-06
6
3,360 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

13 Experts available now in Live!

Get 1:1 Help Now