Wednesday, 2 October 2013

C# - Gradient over picture in picturebox

C# - Gradient over picture in picturebox

I have developed an application in VB.NET that I'm now moving over to C#.
Everything is going smooth so far, but I'm facing one problem.
I have a pictureBox that has a picture in it. Over this picture box I want
a gradient to be that goes from transparent at top down to the color
"control" to blend in with the forms background color. I have allready
done this in VB.net which works, but when im trying to do this in C# the
gradient seems to be drawn, but behind the picture.
Here is what i have tried:
private void PictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Color top = Color.Transparent;
Color bottom = Color.FromKnownColor(KnownColor.Control);
GradientPictureBox(top, bottom, ref PictureBox1, e);
}
public void GradientPictureBox(Color topColor, Color bottomColor, ref
PictureBox PictureBox1, System.Windows.Forms.PaintEventArgs e)
{
LinearGradientMode direction = LinearGradientMode.Vertical;
LinearGradientBrush brush = new
LinearGradientBrush(PictureBox1.DisplayRectangle, topColor,
bottomColor, direction);
e.Graphics.FillRectangle(brush, PictureBox1.DisplayRectangle);
brush.Dispose();
}
However this does in fact seem to work, but again it paints the gradient
behind the picture. In VB.net it painted it on top of the picture without
any extra code..
Do i need to add anything extra?
If it matters im coding in C# 2010 express.

No comments:

Post a Comment