Is it possible to apply Gradient to a Background Image in Silverlight? - silverlight

I want to apply some gradient to my image how can I achieve that?
Thanx

You could host your image within a Border, applying the gradient to the border background:
<Border>
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>
</StackPanel.Background>
<Image Source=..your image source .." />
</Border>
This assumes that your image has some opaque regions which will show the gradient beneath it.

In the gradient you can use one or more colours. Any it is very easy to set the gradient using either VS2010 or expression blend. The following is the sample code of a two-colour gradient with blue and white.
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform> <CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FFB6D3F9"/>
<GradientStop Color="#FFFDFDFD" Offset="1"/>
</LinearGradientBrush>

Related

Vertical line reducing width at the both Ends

I have a requirement to draw a vertical line shown in Image attached. it has a width of say (1 px) Equal width in the middle but at the Dead ends it shoul look like getting disappeared. I tried with line and border but it does not look like getting disappeared at the dead ends.
Something like this;
<Rectangle Width="1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="DarkGray" Offset="0.75"/>
<GradientStop Offset="1"/>
<GradientStop Color="DarkGray" Offset="0.25"/>
<GradientStop Offset="0"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
Could use a Rectangle with a LinearGradient as its Fill brush.

Linear Gradient from bottom to top

I need an Ellipse with a linear gradient from bottom(pink) to top(red).
<Ellipse Width="200" Height="200">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0" >
<GradientStop Color="Pink" Offset="0" />
<GradientStop Color="Red" Offset="1" />
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
The above code shows a gradient from left bottom to top right.
I need the gradient to move from left middle to top middle.
I tried changing the starting and end point with no success. Is there another property that can be used?
It has to be a linear gradient on a ellipse. I cannot use a radial gradient here.
gradient from Bottom to Top (vertical)
<LinearGradientBrush StartPoint="0,1" EndPoint="0,0">
gradient from left middle to top middle
<LinearGradientBrush StartPoint="0,0.5" EndPoint="0.5,0">
Visual representation of how it works

How to create an array of linear gradient brushes in xaml using static resource?

I am trying to create an array of gradient brushes in xaml but I would like to point each array element back to a static resource that is the gradient brush. So, far I've been unable to achieve this. Can someone provide guidance on how I might accomplish this?
I want to add them to an x:Array but point to the resource rather than having to re-define the brushes as I've had to do here. I use the brushes in several places in addition to an array of them so I only want to define the brushes once.
<LinearGradientBrush x:Key="OrangeLinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFF7941D" />
<GradientStop Offset="1" Color="#FFF26522" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="RedLinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#F26263" />
<GradientStop Offset="1" Color="#B80303" />
</LinearGradientBrush>
<x:Array x:Key="BrushArray" Type="LinearGradientBrush">
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFF7941D" />
<GradientStop Offset="1" Color="#FFF26522" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#F26263" />
<GradientStop Offset="1" Color="#B80303" />
</LinearGradientBrush>
</x:Array>
You may use StaticResourceExtensions:
<x:Array x:Key="BrushArray" Type="LinearGradientBrush">
<StaticResourceExtension ResourceKey="OrangeLinearGradientBrush"/>
<StaticResourceExtension ResourceKey="RedLinearGradientBrush"/>
</x:Array>

WPF control that fades out any overflowing text to transparent, rather than clipping it or wrapping?

How can I create a WPF control (similar to a TextBlock) so that any overflowing text is faded out to transparent rather than simply clipping or wrapping?
I need to keep my control fixed-width, so expanding the width of the control to fit the text is not an option. I also do not want to make the text font smaller.
Not quite sure exactly what you are trying to achieve, but you could do something like this:
<TextBlock Text="Some long text here that should fade out">
<TextBlock.Foreground>
<LinearGradientBrush>
<GradientStop Offset="0" Color="Black"/>
<GradientStop Offset="0.7" Color="Black"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
But your control still needs to be wide enough to accommodate all the text for it to display.
<TextBlock Text="fgdfgfdgfddgfdgdfgfdgfdgd" Width="129" TextWrapping="NoWrap">
<TextBlock.Foreground>
<LinearGradientBrush EndPoint="0.661,0.399" StartPoint="0.008,0.496">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#7F000000" Offset="0.803"/>
<GradientStop Color="#4C0A0909" Offset="0.95"/>
<GradientStop Color="#BF000000" Offset="0.729"/>
<GradientStop Color="#F8000000" Offset="0.699"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
The trick on the gradient is that even though all colors are all based off black, The fade is achieved through opacity of each gradient by playing with A part of RGBA, in pseudo-code:
GradientStop Color="Black" A=100%
GradientStop Color="Black" A=97% Offset="0.803"
GradientStop Color="Black" A=75% Offset="0.95"
GradientStop Color="Black" A=80% Offset="0.729"
GradientStop Color="Black" A=30% Offset="0.699"
Thanks guys, but I found the answer I needed on MSDN.

Partial-filling of a rectangle on Silverlight

I am new in Silverlight. I need to fill a rectangle partially with a SolidColorBrush (like a Bar, I need the border). With GradientBrush, the change of color is not sharp.
I can add two rectangles for this, but there has to be a better way.
Any suggestion would be appreciated.
I am using Silverlight 4.0.
Thanks for reading.
-Rakib
What about using gradient brush with gradient stops very close to each other, something like this:
<LinearGradientBrush x:Key="_backgroundBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Yellow" Offset="0.65" />
<GradientStop Color="Blue" Offset="0.66" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>

Resources