473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Trying to Draw an Image onto a Gradient Background

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
1 4082
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Summersnow | last post by:
Hey if anyone could help me this would be great, been staring at the screen for over 2 hours and cant figure out why the image wont, align to the top left and wrap the text around. Any help would...
1
by: Robert W. | last post by:
I've successfully implemented code to change a form's background to a nice blue gradient. It looks great! But I'm having problems adding controls with transparent backgrounds to the form. ...
0
by: Don | last post by:
I'm making a custom control that has a multiline textbox on it. I want to make the textbox have a gradient background. On my custom control's Paint event I call a routine that draws a gradient on...
1
by: zxo102 | last post by:
Hi everyone, I have tried two days to figure out how to draw the image in wx.BufferedDC on the page created by AddPage of wx.Notebook but still got no clue. The attached example works fine. If...
1
by: Daniel Mark | last post by:
Hello all: I am using PIL to draw a rectangle filled with color blue. Is there anyway I could set the fill pattern so that the drawn rectangle could be filled with gradient blue? Thank...
2
by: =?Utf-8?B?UmFodnlu?= | last post by:
Hi all; I have a windows desktop application in which I have set as a background image, a graphic that I whipped up. Its nothing fancey, just some gradient colors, shadows, glow, that sort of...
9
by: Eric Lindsay | last post by:
How do you provide a consistent gradient fill as a background in a liquid layout? If I make a gradient fill image say 1000 pixels wide (and repeat it down the page) to suit a typical computer...
4
by: Bob Altman | last post by:
Hi all, I posted an earlier question to the microsoft.public.inetserver.asp.general newsgroup asking how to set the background of a client-side table cell to a gradient, and I received this...
2
by: studentofknowledge | last post by:
For some unknown reason ie is placing images I have in a div in a weird way. One image is overlapping another but this problem is not occuring in mozilla. I have looked at my code over and over again...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.