Solved

Label transparency

Posted on 2004-04-06
6
3,363 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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