Trying to Draw an Image onto a Gradient Background

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?
 
G

Guest

I've solved my initial problem but now have another question. What I did was
dump the last method and modified the second one to this:

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();

PictureBox pic = pictureLogo;
if (pic.Image != null)
{
Rectangle rect = pic.ClientRectangle;
Rectangle imgRect = new Rectangle(pic.Left, pic.Top, pic.Width,
pic.Height);

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorKey(Color.FromArgb(240,240,240),
Color.White, ColorAdjustType.Bitmap);

e.Graphics.DrawImage(pic.Image, imgRect, 0, 0, rect.Width,
rect.Height, GraphicsUnit.Pixel, imageAttributes);
}
}



It works great. But what I'd ideally like to do is create an animation,
loading successive frames from a resource file. I already have all the code
to do that but whenever I change the Image in pictureLogo, nothing happens.
I had expected that as soon as I changed it, the image would also change.
Why isn't this happening?
 

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