Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Transparent picturebox?

192 views
Skip to first unread message

BlarneyFOR

unread,
Jan 7, 2005, 2:07:41 PM1/7/05
to
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

Sergey Bogdanov

unread,
Jan 7, 2005, 2:16:56 PM1/7/05
to
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

BlarneyFOR

unread,
Jan 7, 2005, 3:00:13 PM1/7/05
to
Thanks Sergey,
I am sort of new to .NET. With the above work for VB.net too?

Sergey Bogdanov

unread,
Jan 7, 2005, 3:22:10 PM1/7/05
to
Sure! It could be easy translated to VB.NET.

Best regards,
Sergey Bogdanov

Daniel Moth

unread,
Jan 7, 2005, 3:25:34 PM1/7/05
to
Check this out if you don't have a book:
http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html

There are also a number of online converters...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"BlarneyFOR" <crumb...@yahoo.com> wrote in message
news:1105128013.0...@f14g2000cwb.googlegroups.com...

Pug

unread,
Jan 7, 2005, 8:35:16 PM1/7/05
to
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.

"BlarneyFOR" <crumb...@yahoo.com> wrote in message

news:1105124861....@z14g2000cwz.googlegroups.com...

Blarney

unread,
Jan 8, 2005, 2:52:25 PM1/8/05
to
On Fri, 07 Jan 2005 22:22:10 +0200, Sergey Bogdanov
<sergey....@gmail.com> wrote:

> 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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Blarney

unread,
Jan 8, 2005, 2:54:46 PM1/8/05
to
On Fri, 7 Jan 2005 20:25:34 -0000, Daniel Moth <dmo...@hotmail.com> wrote:

> Check this out if you don't have a book:
> http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html
>
> There are also a number of online converters...
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>

Thanks for the link (bookmarked). I found a coverter here:
http://www.ragingsmurf.com/vbcsharpconverter.aspx

just not sure where to place the code now. <blush>

Sergey Bogdanov

unread,
Jan 9, 2005, 2:26:38 PM1/9/05
to
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

BlarneyFOR

unread,
Jan 10, 2005, 1:23:54 PM1/10/05
to
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

Sergey Bogdanov

unread,
Jan 10, 2005, 2:09:28 PM1/10/05
to
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

Blarney

unread,
Jan 10, 2005, 9:11:07 PM1/10/05
to
On Mon, 10 Jan 2005 21:09:28 +0200, Sergey Bogdanov
<sergey....@gmail.com> wrote:

> 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!

BlarneyFOR

unread,
Jan 13, 2005, 4:08:24 PM1/13/05
to
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?

Sergey Bogdanov

unread,
Jan 13, 2005, 4:20:56 PM1/13/05
to
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

BlarneyFOR

unread,
Jan 14, 2005, 9:17:42 AM1/14/05
to
Brilliant! Worked first time! Thank you very much!

0 new messages