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

Transparent Picturebox

421 views
Skip to first unread message

grub...@gmail.com

unread,
Apr 9, 2006, 8:57:24 PM4/9/06
to
Despite searching for the past 2 days I have not found any solution to
what should be a simple task:
Create a PictureBox which renders its Image with an alpha value so that
other control underneath it can show through, similar to what you get
by setting a Forms opacity.

The usual recommendations are in place:
cp.ExStyle |= 0x20;
SetStyle( ControlStyles.UserPaint, true );
SetStyle( ControlStyles.AllPaintingInWmPaint, true );
SetStyle( ControlStyles.OptimizedDoubleBuffer, false );

For debugging purposes the OnPaint and Background methods are empty:
protected override void OnPaint( PaintEventArgs pe )
{ return; }
protected override void OnPaintBackground()
{ return; }


By updating myPictureBox.Location I move it over other controls in the
same form. Now with the paint methods empty I would expect nothing to
appear on the form, but instead Windows seems to automatically blit
part of the form from wherever the PictureBox was first made visible.
How do I prevent this ?

Regards,
Marco

Bob Powell [MVP]

unread,
Apr 12, 2006, 2:25:17 PM4/12/06
to
You can't do this with PictureBox. You should be able to do it with a custom
control. See the GDI+ FAQ and Windows Forms Tips and Tricks for details

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<grub...@gmail.com> wrote in message
news:1144630644.2...@g10g2000cwb.googlegroups.com...

grub...@gmail.com

unread,
Apr 12, 2006, 5:39:20 PM4/12/06
to
Thank you, Bob. I had read your article but was confused by the timer
event when reading the source. I think now I get it and will try it
later tonight.

grub...@gmail.com

unread,
Apr 15, 2006, 5:15:43 AM4/15/06
to
Looks like it's working now. Except for the ugly flicker while
redrawing. Here's the C# version of an alpha-blended PictureBox:

/// <summary>
/// This control works like a PictureBox but blends images with
background
/// </summary>
public class PanelEx : Panel
{
public Image Image;

/// <summary>
/// Mark window as transparent
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20; //WS_EX_TRANSPARENT
return cp;
}
}

public PanelEx()
{
Opacity = 0.5f;
}

/// <summary>
/// This is important: redraw all other controls under PanelEx first
/// </summary>
public void InvalidateEx()
{
if (Parent == null)
return;
Rectangle rc = new Rectangle( this.Location, this.Size );
Parent.Invalidate( rc, true );
}

protected float opacity;
protected ImageAttributes imgAttributes;
/// <summary>
/// 1.0 == no transparency, 0.0 == fully transparent
/// </summary>
public float Opacity
{
get { return opacity; }
set
{
opacity = value;
if (imgAttributes != null)
imgAttributes.Dispose();
// define alpha coefficient
float[][] colorMatrixElements = {
new float[] {1, 0, 0, 0, 0}, //r
new float[] {0, 1, 0, 0, 0},//g
new float[] {0, 0, 1, 0, 0},//b
new float[] {0, 0, 0, value, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix( colorMatrixElements );
imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap );
}
}

/// <summary>
/// draw blended Image
/// </summary>
/// <param name="pe"></param>


protected override void OnPaint( PaintEventArgs pe )
{

Bitmap bmp = Image as Bitmap;
if (bmp == null)
return;

pe.Graphics.DrawImage(
Image,
new Rectangle( 0, 0, Size.Width, Size.Height ), // destination
rectangle
0, 0, // upper-left corner of source rectangle
Image.Width, // width of source rectangle
Image.Height, // height of source rectangle
GraphicsUnit.Pixel,
imgAttributes );
}
protected override void OnPaintBackground( PaintEventArgs pevent )
{
// left intentionally blank!
}

protected override void OnLocationChanged( EventArgs e )
{
base.OnLocationChanged( e );
InvalidateEx(); // redraw everything underneath this control
}
}

0 new messages