0

I trying to get the same color gradient in C# code

        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="#FF4557BA" Offset="1"/>
        </LinearGradientBrush>

So far I have this but it is wrong(can't find how to enter in Hex so I tried argb)

  LinearGradientBrush gradient = new LinearGradientBrush();
    gradient.StartPoint = new Point( 0, 0 );
    gradient.EndPoint = new Point( 1, 1 );

    GradientStop color1 = new GradientStop();
    color1.Color = Colors.Black;
    color1.Offset = 0;
    gradient.GradientStops.Add(color1);

    GradientStop color2 = new GradientStop();
    color2.Color = Color.FromArgb(100,69,87,186);
    color2.Offset = 1;
    gradient.GradientStops.Add( color2 );

Edit

I am trying to do this in wp7 where I have this gradient in a property that I will bind to the "background" of my controls.

I however it just seems like I get a solid color and not the gradient.

chobo2
  • 83,322
  • 195
  • 530
  • 832

3 Answers3

1
You can try like this
 Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

by refering 
using System.Windows.Media;
Debashrita
  • 940
  • 1
  • 8
  • 20
0

So far this is what I got:

  LinearGradientBrush gradient = new LinearGradientBrush();
    gradient.StartPoint = new Point( 0.5, 0 );
    gradient.EndPoint = new Point( 0.5, 1 );

    gradient.GradientStops.Add(new GradientStop(Colors.Black, 0));
    gradient.GradientStops.Add(new GradientStop(Color.FromArgb(100,69,87,186), 1));


    whatevercontrolyougot.Fill = gradient;

Works fine here.

A screenshot:
enter image description here

trinaldi
  • 2,872
  • 2
  • 32
  • 37
0

The Problem is that you are refereeing alpha channel as 100 in code and in xamal you have 0xFF==255

so use this line in code

Color.FromArgb(0xFF, 0x45, 0x57, 0xBA);

C# allows you to enter values in Decimal, Binary or Hex,

Mujahid Daud Khan
  • 1,983
  • 1
  • 14
  • 23