Image Flipping in WPF - wpf

How do you get an image to be reflected in wpf without using the xaml? I have a rectangle with a BitmapImageBrush as its fill. I want to be able to regularly flip this image (X-axis) back and forth at will in C#. I've read about using the ScaleTransform property but this only seems to work in the xaml and that's not what I want.
This is how the image is created:
ImageBrush l = new ImageBrush(new BitmapImage(new Uri(uriPath, UriKind.Relative)));
_animation.Add(l);
_visual = new Rectangle();
_visual.Fill = _animation[0];
_visual.Width = width;
_visual.Height = height;
_visual.Visibility = Visibility.Visible;
_window.GameCanvas.Children.Add(_visual);
_animation is a list of ImageBrushes.
This is really simple, yet I can't seem to figure out how to do it. Please help.

You can still add a scale transform programmatically, rather than using xaml.
For instance, in this article.
To do the flip, set your scale transform to be negative in the direction you want to flip (ie, if horizontal, set x to -1, or -desiredScale if you also want to resize).

Related

Animate a combined geometry along a path

I have WPF/VB application that animates an ellipse geometry along a path using point animation. I used PointAnimationUsingPath and a Storyboard as per this MSDN example and it works great.
I now want to show a number inside the ellipse. To do this I created a combined geometry and set my ellipse as geometry1. I then created a formattedtext(...).buildgeometry for my number and set that as geometry2. Like this:
Dim CarGeo AS New CombinedGeometry()
CarGeo.Geometry1 = New EllipseGeometry(StartPoint, 5, 5)
CarGeo.Geometry2 = New FormattedText(carIndex.ToString, System.Globalization.CultureInfo.GetCultureInfo("en-us"), Windows.FlowDirection.LeftToRight, New Typeface("Veranda"), 7, Brushes.White).BuildGeometry(New Point(StartPoint.X - 4, StartPoint.Y - 4))
The resulting geometry is exactly what I wanted.
The problem is I don't seem to be able to animate this geometry along my path because unlike an ellipse there is no center property in a combined geometry to set the targeted property to on the StoryBoard.
' Create a PointAnimationgUsingPath to move the car along the animation path.
cpAnimation = New PointAnimationUsingPath
cpAnimation.PathGeometry = pgSectorPath(intSector)
cpAnimation.Duration = timDuration
' Set the animation to target the Center property of the EllipseGeometry
Storyboard.SetTargetName(cpAnimation, "CarGeo")
Storyboard.SetTargetProperty(cpAnimation, New PropertyPath(EllipseGeometry.CenterProperty))
Is there I property in a combined geometry that I can use for the animation?
If not can I wrap the geometry in something else that can be animated?
I'm very new to WPF and have wasted way too much time searching for an answer to this. Any help would be greatly appreciated.
Just wrap the CombinedGeometry into a Path object, as in the MS example:
http://msdn.microsoft.com/en-us/library/system.windows.media.combinedgeometry(v=vs.110).aspx

Is it possible to change an ImageBrush color inside WPF?

I'm trying to do something quite simple, but I'm not sure if it's possible. I have this image:
Inside my board game project, I use this to indicate the mouse position in the board and stuff like that (being the board a 8x8 grid with a rectangle inside). I'm also setting the grid cell background brush as, say, a SolidColorBrush with Blue color and .25 opacity, while the rectangle that lies above it with the target, or vice-versa, because I couldn't achieve my desired effect, which is, the part that is black to turn full blue, as my brush, while the rest remains with the opacity and etc. The way I did, it was possible to make the black part become blueish, with a transluscent background, but the color in the border isn't quite right yet:
I don't know if I was able to make myself clear, but I guess the picture shows what is my goal.
The code:
ImageBrush targetBrush = new ImageBrush();
targetBrush.ImageSource = new BitmapImage(new Uri("Resources/GameScreen/selectedTile.png", UriKind.Relative));
targetBrush.Opacity = 1;
SolidColorBrush backBrush = new SolidColorBrush(Colors.Blue);
backBrush.Opacity = .25;
tile.Children.Add(new Rectangle());
foreach (object target in tile.Children)
{
if (target.GetType() == typeof(Rectangle))
{
((Rectangle)target).Fill = backBrush;
}
}
tile.Background = targetBrush;
Thanks in advance.
As the image is just a mask you could use a Rectangle with a blue Fill and an OpacityMask set to your image.

Translate and scale animation

I have a wpf application and image inside a canvas. The image is placed in 0,0.
I need to animate the image moving from 0,0 to 500,200 and in the same time growing (I like to make an effect like coming from far to near).
If I do this:
TranslateTransform ttx = new TranslateTransform();
TranslateTransform tty = new TranslateTransform();
DoubleAnimationUsingKeyFrames dax = new DoubleAnimationUsingKeyFrames();
dax.KeyFrames.Add(new LinearDoubleKeyFrame(500, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
DoubleAnimationUsingKeyFrames day = new DoubleAnimationUsingKeyFrames();
day.KeyFrames.Add(new LinearDoubleKeyFrame(200, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
TransformGroup tg = new TransformGroup();
tg.Children.Add(ttx);
tg.Children.Add(tty);
krug.RenderTransform = tg;
ttx.BeginAnimation(TranslateTransform.XProperty, dax);
tty.BeginAnimation(TranslateTransform.YProperty, day);
And this works fine. It animates the translation of the image "krug" from 0,0 to 500,200.
But when I add logic for zooming the image while translating like this:
ScaleTransform zoom = new ScaleTransform();
DoubleAnimationUsingKeyFrames zoomTimeline = new DoubleAnimationUsingKeyFrames();
zoomTimeline.KeyFrames.Add(new LinearDoubleKeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
tg.Children.Add(zoom);
zoom.BeginAnimation(ScaleTransform.ScaleXProperty, zoomTimeline);
zoom.BeginAnimation(ScaleTransform.ScaleYProperty, zoomTimeline);
Then the image does not stop to 500, 200 but goes more far. If the zoom factor is bigger, the translation goes more future. How can I control the animation to stop at 500,200 ?
The problem you run into when combining scale and translate transforms is that it will scale the translate transform from the point of origin(ScaleTransform.CenterX, ScaleTransform.CenterY)
For example, if you want to slide it to the right by 50, and double it's scale, it will actually move a net distance of 100.
Try animating the ScaleTransform.CenterX and ScaleTransform.CenterY to match your translate transform. I believe that would let you scale on the fly like you want.
Put the animations in a single storyboard and set the duration of the storyboard to control the length of the animations.
Then start the storyboard.
I found a solution. I created separate user control that only does ScaleTransform. Then I apply TranslateTransform on the user control.

Draw custom pushpin in code without using an image

I need to draw a pushpin for the Bing Silverlight control that can have the head be a variable color. I can draw a nice dot like this:
Dim marker As Ellipse = New Ellipse
marker.Fill = New SolidColorBrush(Color.FromArgb(255, 11, 255, 0))
marker.Stroke = New SolidColorBrush(Colors.Gray)
marker.Width = 10
marker.Height = 10
I'll be making dozens of pushpins, each with a slightly different color for the Fill. How can I add the pointy part? I would like to have some amount of flaring out at the top so that it looks more like a pushpin and less like a lollipop.
Examples in C# are welcome as well.
Maybe there's a reason you need it to be a custom one rather than using the built-in pushpin objects, but if not, you can set the background color on those pushpins like so:
myPushpin.Background = new SolidColorBrush(Colors.Gray);
As far as actually drawing your own, I'm not as sure. Could you draw some sort of a triangle?

Drawing Basic Shapes in WPF InkCanvas

I am working on a paint like application in wpf.I want the users to be able to add some drawings over images or plain surfaces.Also i want to draw some basic shapes like line,ellipse or a rectangle.I am trying to work with an inkcanvas,where i can do freehand drawing,but i cant draw shapes like in paint.Can anyone guide me and provide some clues on how to do it.Please help me on this.Any inputs will be greatly appreciated.
There are two sorts of collections in an InkCanvas:
Strokes, which are composed of StylusPoints and defined by DrawingAttributes. That's what the Ink is, as drawn by a mouse or stylus.
The other is Children, which can contain FrameworkElements. Ellipse, for instance, is a Shape is a FrameworkElement.
Try playing around with yourCanvas.Children.Add(ellipse) and see how you go. There is certainly no reason to shy away from the InkCanvas just because you also want to use predefined shapes.
It's worth pointing out, though, that the InkCanvas's little brother, the InkPresenter, does NOT have a Children property. And Silverlight only has that one.
WPF provides a Shape class that includes prebuilt methods that you can draw shapes with. Don't use the inkcanvas and instead draw directly to a canvas.
Here http://ciintelligence.blogspot.com/2011/07/silverlight-drawing-tool-silver-draw.html
you can find better control which improved SilverDraw control with extra features:
Freatures are:
* You can draw basic shapes and also can draw using freehand pencil.
* You can erase drawing.
* You can undo and redo drawing.
* Can save drawing as jpeg in server side.
Here is a simple implementation:
public void drawCircleAp(Double EHeight, Double EWidth, InkCanvas surface)
{
Ellipse e1 = new Ellipse();
e1.Width = EWidth;
e1.Height = EHeight;
var brush = new SolidColorBrush();
brush.Color = Color.FromArgb(100, 0, 0, 0);
e1.Stroke = brush;
e1.StrokeThickness = 4;
surface.Children.Add(e1);
}

Resources