G
Guest
In my Winforms app I'm trying to get an image's background to appear
transparent on a form that has a gradient background. So I added a
PictureBox and then attempted to add a custom paint command for the
PictureBox. But it's not working. Here's the code I've written:
public void InitializeLinearGradients()
{
this.Paint += new PaintEventHandler(PaintClient);
this.SizeChanged += new EventHandler(SizeClient);
pictureLogo.Paint += new PaintEventHandler(PaintPictureBox);
}
protected void SizeClient(Object sender, EventArgs e)
{
sender = sender as Form;
this.Invalidate();
}
protected void PaintClient(Object sender, PaintEventArgs e)
{
Form frm = sender as Form;
e.Graphics.Clip = new Region(frm.ClientRectangle);
LinearGradientBrush lgb = new LinearGradientBrush(frm.ClientRectangle,
Color.LightBlue, Color.FromArgb(0,0,160), 90F, false);
e.Graphics.FillRectangle(lgb, frm.ClientRectangle);
lgb.Dispose();
}
protected void PaintPictureBox(Object sender, PaintEventArgs e)
{
PictureBox pic = sender as PictureBox;
e.Graphics.Clip = new Region(pic.ClientRectangle);
Rectangle rect = pic.ClientRectangle;
ImageAttributes imageAttributes = new ImageAttributes();
Color color = Color.FromArgb(125, 255, 255, 255);
imageAttributes.SetColorKey(color, color, ColorAdjustType.Bitmap);
e.Graphics.DrawImage(pic.Image, rect, 0, 0, rect.Width, rect.Height,
GraphicsUnit.Pixel, imageAttributes);
}
// End of code
It's the last method that isn't working properly. I tried to introduce an
Alpha channel but I do confess that I really don't know how to handle it.
Any ideas how I could resolve this?
transparent on a form that has a gradient background. So I added a
PictureBox and then attempted to add a custom paint command for the
PictureBox. But it's not working. Here's the code I've written:
public void InitializeLinearGradients()
{
this.Paint += new PaintEventHandler(PaintClient);
this.SizeChanged += new EventHandler(SizeClient);
pictureLogo.Paint += new PaintEventHandler(PaintPictureBox);
}
protected void SizeClient(Object sender, EventArgs e)
{
sender = sender as Form;
this.Invalidate();
}
protected void PaintClient(Object sender, PaintEventArgs e)
{
Form frm = sender as Form;
e.Graphics.Clip = new Region(frm.ClientRectangle);
LinearGradientBrush lgb = new LinearGradientBrush(frm.ClientRectangle,
Color.LightBlue, Color.FromArgb(0,0,160), 90F, false);
e.Graphics.FillRectangle(lgb, frm.ClientRectangle);
lgb.Dispose();
}
protected void PaintPictureBox(Object sender, PaintEventArgs e)
{
PictureBox pic = sender as PictureBox;
e.Graphics.Clip = new Region(pic.ClientRectangle);
Rectangle rect = pic.ClientRectangle;
ImageAttributes imageAttributes = new ImageAttributes();
Color color = Color.FromArgb(125, 255, 255, 255);
imageAttributes.SetColorKey(color, color, ColorAdjustType.Bitmap);
e.Graphics.DrawImage(pic.Image, rect, 0, 0, rect.Width, rect.Height,
GraphicsUnit.Pixel, imageAttributes);
}
// End of code
It's the last method that isn't working properly. I tried to introduce an
Alpha channel but I do confess that I really don't know how to handle it.
Any ideas how I could resolve this?