How do I go about animating the opacity of a dropshadoweffect in code. I am dynamically creating stackpanels in my UI which contain an image and a text block. I have applied a dropshadoweffect to the image, which I would like to animate the opacity. Here's what I have:
Image img = ch.ChannelLogo; //Create the image for the button
img.Height = channelStackPnl.Height * .66;
DropShadowEffect dSE = new DropShadowEffect();
dSE.Color = Colors.White;
dSE.Direction = 25;
dSE.ShadowDepth = 10;
dSE.Opacity = .40;
img.Effect = dSE;
DoubleAnimation animateOpacity = new DoubleAnimation();
animateOpacity.From = 0;
animateOpacity.To = 1;
animateOpacity.AutoReverse = true;
animateOpacity.RepeatBehavior = RepeatBehavior.Forever;
animateOpacity.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
//Here's where I am stuck. How do I specifically target the opacity of the effect propery?
img.BeginAnimationimg.BeginAnimation(DropShadowEffect.OpacityProperty,animateOpacity);
dSE.BeginAnimation(DropShadowEffect.OpacityProperty, animateOpacity);
Probably...
(You are animating the effect, not the image)
Related
I need a smooth animation of window transparency. I added this code to the "LOADED" event of the window.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 100.0;
myDoubleAnimation.To = 0.1;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
Storyboard.SetTargetName(myDoubleAnimation, Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(UIElement.OpacityProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Begin(this);
There is a sharp jump of transparency. Animation is missing. Where there has been a mistake?
Opacity is a relative value in the range 0 .. 1. Use
myDoubleAnimation.From = 1.0;
or don't set it at all.
Instead of using a Storyboard, you may also just write
BeginAnimation(OpacityProperty, new DoubleAnimation
{
To = 0.1,
Duration = TimeSpan.FromSeconds(1)
});
I'm working to make a ColorPicker for my WPF app (I know about Extended Toolkit) and I've copied the xaml from this and it's working well but now I need to get the color is pointing the mouse. As hue is inside Canvas I've tried this:
Rectangle o = sender as Rectangle;
Canvas picker = ((Canvas)((VisualBrush)o.Fill).Visual);
Point point = e.GetPosition(picker);
RenderTargetBitmap render = new RenderTargetBitmap((int)picker.RenderSize.Width, (int)picker.RenderSize.Height, 96, 96, PixelFormats.Default);
render.Render(picker);
image.Source = render;
if ((point.X <= render.PixelWidth) && (point.Y <= render.PixelHeight)) {
var crop = new CroppedBitmap(render, new Int32Rect((int)point.X, (int)point.X, 1, 1));
var pixels = new byte[4];
crop.CopyPixels(pixels, 4, 0);
selectedColor = new SolidColorBrush(Color.FromArgb(255, pixels[2], pixels[1], pixels[0]));
selectedColorRect.Fill = selectedColor;
}
}
on my mouse move event but render.Render(picker) won't render the opacity mask used to make the gradient.
EDIT
The left is the original rectangle and in the right side is the result of the render.Render(picker); image.Source = render;, as you can see the right side is not applying the opacity mask.
How to scale dynamic image control from the center programmatically in c# for Windows 8.1. This code scales image from left upper corner:
Image img=new Image(){Width=150,Height=150};
img.RenderTransform = new CompositeTransform();
Storyboard story = new Storyboard();
DoubleAnimation xAnim = new DoubleAnimation();
DoubleAnimation yAnim = new DoubleAnimation();
xAnim.From = 0;
yAnim.From = 0;
xAnim.To = 1;
yAnim.To = 0.5;
xAnim.Duration = TimeSpan.FromMilliseconds(1000);
yAnim.Duration = TimeSpan.FromMilliseconds(1500);
story.Children.Add(xAnim);
story.Children.Add(yAnim);
Storyboard.SetTarget(xAnim, img);
Storyboard.SetTarget(yAnim, img);
Storyboard.SetTargetProperty(xAnim, "(UIElement.RenderTransform).(CompositeTransform.ScaleX)");
Storyboard.SetTargetProperty(yAnim, "(UIElement.RenderTransform).(CompositeTransform.ScaleY)");
story.Begin();
You want
img.RenderTransformOrigin = new Point(0.5, 0.5);
That will mean that all of your transforms will be relative to the center of your image. See the RenderTransformOrigin documentation for more details.
I want to show a brush on my stackpanel in the following behavior:
Brush is an image 240x120
panel is 240x60
I want to show a part of the brush like rect(0, 30, 240, 60) (so that the image on the panel is moved down a bit)
tried it with viewport and Viewbox with no result (empty Panel)
This is my code:
for (int i = 0; i < listExplorationData.Count; i++)
{
StackPanel panelLoop = new StackPanel();
panelLoop.Name = "panel_" + i.ToString();
panelLoop.Width = 240;
panelLoop.Height = 60;
panelLoop.Margin = new Thickness(0, 60 * i, 0, 0);
BitmapImage image = new BitmapImage(
new Uri("pack://application:,,,/GW2-MyWorldExploration;component/Images/" +
listExplorationData[i].mapname_en.Replace(" ", "_") +
"_loading_screen.jpg"));
ImageBrush brush = new ImageBrush();
brush.ImageSource = image;
brush.Stretch = Stretch.None;
brush.Viewport = new Rect(0, 30, 240, 60);
panelLoop.Background = brush;
mainStackPanel.Children.Add(panelLoop);
}
In order to show part of the ImageBrush you would have to set the Viewbox. If you want to specify the viewbox in absolute units, you would also have to set the ViewboxUnits property:
brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 30, 240, 60);
I thought this would be something simple but so far i found nothing. How do you do it?
The accepted answer is now obsolete. Now you may use:
UIElement uie = ...
uie.Effect =
new DropShadowEffect
{
Color = new Color {A = 255, R = 255, G = 255, B = 0},
Direction = 320,
ShadowDepth = 0,
Opacity = 1
};
To achieve the exact same effect as the accepted answer.
just try this
// Get a reference to the Button.
Button myButton = new Button();
// Initialize a new DropShadowBitmapEffect that will be applied
// to the Button.
DropShadowBitmapEffect myDropShadowEffect = new DropShadowBitmapEffect();
// Set the color of the shadow to Black.
Color myShadowColor = new Color();
myShadowColor.ScA = 1;
myShadowColor.ScB = 0;
myShadowColor.ScG = 0;
myShadowColor.ScR = 0;
myDropShadowEffect.Color = myShadowColor;
// Set the direction of where the shadow is cast to 320 degrees.
myDropShadowEffect.Direction = 320;
// Set the depth of the shadow being cast.
myDropShadowEffect.ShadowDepth = 25;
// Set the shadow softness to the maximum (range of 0-1).
myDropShadowEffect.Softness = 1;
// Set the shadow opacity to half opaque or in other words - half transparent.
// The range is 0-1.
myDropShadowEffect.Opacity = 0.5;
// Apply the bitmap effect to the Button.
myButton.BitmapEffect = myDropShadowEffect;
#Gleno's answer helped me the most. In my case I was using it for visual feedback on a missed form item. To then remove the dropshadow I used:
myComboBox.ClearValue(EffectProperty);
in a selectionChanged event.
Hope this helps someone. I had to search for a bit.