Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Label transparency

Posted on 2004-04-06
6
3,364 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

792 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