I have some picture boxes on Visual Basic Express 2010, and these picture have alpha channels on them, but when the background color is set to transparent, it does not become transparent but becomes the color of the form. I still can't see anything else through the alpha map. What is the problem? I don't only want to see the parent object behind the picture box, but every other object that is underneath it.
-
1What color would you expect it to be, if not the color of the form? – vesan Sep 07 '15 at 04:55
-
@vesan I want to be able to see other images and stuff under them – christai Sep 07 '15 at 04:57
-
Other images on the form, or your desktop and other applications? Can you post a screenshot or a mockup of what you want it to look like? – vesan Sep 07 '15 at 05:06
-
possible duplicate of [Transparency of picture box](http://stackoverflow.com/questions/32241014/transparency-of-picture-box) – Zohar Peled Sep 07 '15 at 15:49
1 Answers
I have got some code that will create "proper" transparency for a control by drawing every control that's behind it onto it's background.
How to use:
1) Create a custom class. (From the "Add New Item" menu)
2) Give it a name of your choice (ex: TransparentPictureBox
)
3) Make it inherit from the original PictureBox.
Public Class TransparentPictureBox
Inherits PictureBox
End Class
4) Paste this code within the class:
Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
If Parent IsNot Nothing Then
Dim index As Integer = Parent.Controls.GetChildIndex(Me)
For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
Dim c As Control = Parent.Controls(i)
If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
c.DrawToBitmap(bmp, c.ClientRectangle)
e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
bmp.Dispose()
End If
Next
End If
End Sub
The code will override the PictureBox's OnPaintBackground
event, thus drawing it's own transparent background.
5) Build your project.
6) Select your component from the ToolBox and add it to your form.
Hope this helps!
Result:
EDIT:
In addition to your comment, start by building your project via the Build
> Build <your project name>
menu.
Then you can find your custom control at the top of the toolbox under the <your project name> Components
category.

- 18,045
- 5
- 28
- 75
-
-
Open the `Build` menu in Visual Studio and press `Build
`. After that you can find it in the toolbox under ` – Visual Vincent Sep 07 '15 at 15:17Components`. -
-
Okay, but when something underneath the picture box changes, it still appears the same as when the form is started... How can it be Refreshed as such? – christai Sep 08 '15 at 02:33
-
1