2

I saw a way to make a form's background color Gradient.

That was done by a GradientBrush but when I try that, it says it doesn't exist.

I wrote like this:

GradientBrush something = New GradientBrush();

In the output window I see the "doesn't exist in the current context" error.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Talha Talip Açıkgöz
  • 1,821
  • 4
  • 15
  • 28
  • Add the code you are trying to do. – Abin Sep 16 '15 at 17:23
  • What are you targetting: Winforms? WPF? ASP? ...?? __Always__ tag your question accordingly! - As the `GradientBrush` is not recognized I assum Winforms. See my answer how to create a simple gradient in Winforms! – TaW Sep 16 '15 at 23:01

2 Answers2

1

You might want to add the System.Windows.Media nampespace to your application simply add.

using System.Windows.Media;

Then the compiler would recoginize this class.

Goodluck.

Slashy
  • 1,841
  • 3
  • 23
  • 42
0

In a winforms Form you could do this:

using  System.Drawing.Drawing2D;
...
...

private void Form1_Paint(object sender, PaintEventArgs e)
{
    using (LinearGradientBrush br = new
            LinearGradientBrush(Form1.ClientRectangle, Color.Wheat, Color.DimGray, 0f))
        e.Graphics.FillRectangle(br, Form1.ClientRectangle);
}

To get rid of flicker set the form to DoubleBuffered = true;

For more colors use the multicolor overload of the LinearGradientBrush! For an example see here!

enter image description here

If the background is fixed you may consider creating a Bitmap with the gradient. Perfect if the user will not resize the form..:

Bitmap form1Back = new Bitmap(form1.ClientSize.Width, form1.ClientSize.Height);
using (Graphics G = Graphics.FromImage(form1Back))
using (LinearGradientBrush br = new 
       LinearGradientBrush( form2.ClientRectangle, Color.Wheat, Color.DimGray, 0f))
    G.FillRectangle(br, form2.ClientRectangle);
form1.BackgroundImage = form1Back;
Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111