?
Solved

Label transparency

Posted on 2004-04-06
6
Medium Priority
?
3,369 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
[X]
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
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 2000 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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 
LVL 19

Expert Comment

by:Fahad Mukhtar
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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