By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

Trying to Draw an Image onto a Gradient Background

P: n/a
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Robert W." wrote:
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.