Transparent picturebox?

B

BlarneyFOR

Hello,

I have been trying to make a picturebox transparent. But even though
there is no graphics file in it yet, it still shows up as white. I am
using this code on a newly created picturebox:

me.PictureBox6.BackColor = system.Drawing.Color.Transparent

Why wouldn't that make my picturebox the same color as the panel or
background image? (I've tried both popping it over an image, and
another picturebox.)

Please help!
Thanks
 
S

Sergey Bogdanov

Sorry but standart PictureBox doesn't support transparent background. To
avoid this limitation you can derive from this and override OnPaint event:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(Color.FromArgb(255, 0, 255), Color.FromArgb(255, 0, 255));
e.Graphics.DrawImage(this.Image, this.ClientRectangle, 0, 0,
this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, attr);
}

The color 255, 0, 255 (RGB) is a transparent.


Best regards,
Sergey Bogdanov
 
B

BlarneyFOR

Thanks Sergey,
I am sort of new to .NET. With the above work for VB.net too?
 
S

Sergey Bogdanov

Sure! It could be easy translated to VB.NET.

Best regards,
Sergey Bogdanov
 
P

Pug

You may find that the compact framework does not support transparent
backgrounds for quite a few if not all the standard controls, picturebox
included.
I found this with labels as well and ended up writing text directly to a
form in its onpaint event to get the desired effect.
 
B

Blarney

Sure! It could be easy translated to VB.NET.

Best regards,
Sergey Bogdanov

Thanks, I found an online converter. One more question. Where do I put
the code and call it? Sorry...I am just not clear what I am doing here.
 
S

Sergey Bogdanov

Create new class (assume it will be named PictureBoxEx), derive it from
PictureBox. In the class of you form write something like this:

Dim pb As PictureBoxEx = New PictureBoxEx()

....

' Your form constructor code
pb.Location = New System.Drawing.Point(0, 0)
pb.Size = New System.Drawing.Size(240, 240)
....

Controls.Add(pb);

Hope this help,
Sergey Bogdanov
 
B

BlarneyFOR

Thanks Sergey, Could you take a look at my code for me? I tried
creating a new project named transparency from scratch and it crashes
if I run it. Crash is a 'Stackoverflowexception in default domain'

'My Form1 code (form1 is where I'd want the transparent picturebox):

Imports transparency.PictureboxEX.ControlCollection

Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.

Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
'
'PictureBox1
'
Dim pb As PictureboxEX = New PictureboxEX
pb.Location = New System.Drawing.Point(0, 0)
pb.Size = New System.Drawing.Size(240, 240)

Me.PictureBox1.Location = New System.Drawing.Point(56, 88)
Me.Picturebox1.Controls.Add(pb)
'
'Form1
'
Me.ClientSize = New System.Drawing.Size(242, 272)
Me.Controls.Add(Me.PictureBox1)
Me.Text = "Form1"

End Sub


'My PictureboxEX class code:

Imports System.Drawing.Imaging.ImageAttributes

Public Class PictureboxEX

Inherits System.Windows.Forms.PictureBox

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim attr As Imaging.ImageAttributes = New
Imaging.ImageAttributes
attr.SetColorKey(Color.FromArgb(255, 0, 255),
Color.FromArgb(255, 0, 255))
e.Graphics.DrawImage(Me.Image, Me.ClientRectangle, 0, 0,
Me.Image.Width, Me.Image.Height, GraphicsUnit.Pixel, attr)
End Sub


End Class
 
S

Sergey Bogdanov

You forgot to include Image to your picture box - include your .bmp file
into the solution and set property "Build Action" to Embedded
Resource. Also I have found some errors:

Public Class Form1
Inherits System.Windows.Forms.Form
Protected WithEvents pb As PictureboxEX = New PictureboxEX

....

Private Sub InitializeComponent()
pb.Location = New System.Drawing.Point(0, pb.Size = New
System.Drawing.Size(240, 240)
pb.Image = New
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YOUAPPLICATIONNAME.Bitmap1.bmp"))
Me.Controls.Add(pb)
....


Best regards,
Sergey Bogdanov
 
B

Blarney

You forgot to include Image to your picture box - include your .bmp file
into the solution and set property "Build Action" to Embedded
Resource. Also I have found some errors:

Public Class Form1
Inherits System.Windows.Forms.Form
Protected WithEvents pb As PictureboxEX = New PictureboxEX

...

Private Sub InitializeComponent()
pb.Location = New System.Drawing.Point(0, pb.Size = New
System.Drawing.Size(240, 240)
pb.Image = New
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YOUAPPLICATIONNAME.Bitmap1.bmp"))
Me.Controls.Add(pb)
...


Best regards,
Sergey Bogdanov

Thank you Sergey! It worked! In fact it worked so well my embedded image
is transparent all the way down to the today screen... I figure I'll be
able to tweak it so that it doesn't show through the project.

thanks again!
 
B

BlarneyFOR

Hi, I am back. I am still having a bit of trouble. When I create an
image using photoshop with the RGB (255, 0, 255), the transparent parts
show through all the way to the today screen. In other words if I have
a picturebox sitting on another picturebox or form, you can see the
today screen through the transparent parts.
Am I doing something wrong in the photoshop image? Or is it the code?
 
S

Sergey Bogdanov

Due to the fact that OnPaintBackground method doesn't draw background
for a size of a picture box you should override it:

protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
SolidBrush sb = new SolidBrush(Parent.BackColor);
e.Graphics.FillRectangle(sb, this.ClientRectangle);
}

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top