Code in WPF can't rotate image


Matt

I have a situation where I want to rotate a control on a WPF form on a left mouse button down event. I've tried to adapt code I found elsewhere, but I haven't gotten there yet.

Here is what I have:

XAML:

            <Image
                Name="UpArrow"
                Height="50"
                Width="50"
                RenderOptions.BitmapScalingMode="HighQuality"
                VerticalAlignment="Top"
                HorizontalAlignment="Left" 
                Margin="10,70,0,0">
                <Image.Source>
                    <TransformedBitmap Source="C:\Some Source File.jpg" >
                        <TransformedBitmap.Transform>
                            <RotateTransform Angle="180"/>
                        </TransformedBitmap.Transform>
                    </TransformedBitmap>
                </Image.Source>
            </Image>

In my code behind:

Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
    Dim TransBitmap As TransformedBitmap = Me.UpArrow.Source
    Dim Trans As RotateTransform = TransBitmap.Transform
    Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
    Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub

I've been trying a few different things but nothing seems to work. I can read C# code well, so if you'd like to answer that way, go ahead, we're only using VB here. I'm also totally able to do this in XAML, but I can't seem to get it to work either.

Thanks!

McGanagar

I think the problem is that the object you want to animate (the transformed bitmap) cannot be animated:

TransformedBitmap implements the ISupportInitialize interface to optimize initialization on multiple properties. Property changes can only happen during object initialization. A call to BeginInit indicates that initialization has started, and a call to EndInit indicates that initialization is complete. After initialization, property changes are ignored.

The same code will work if you target the image itself to a RenderTransform:

<Image
    Name="UpArrow"
    Height="50"
    Width="50"
    MouseLeftButtonDown="UpArrow_MouseLeftButtonDown"
    RenderOptions.BitmapScalingMode="HighQuality"
    VerticalAlignment="Top"
    HorizontalAlignment="Left" 
    Margin="10,70,0,0">
    <Image.RenderTransform>
        <RotateTransform Angle="180"/>
    </Image.RenderTransform>
    <Image.Source>
        <TransformedBitmap Source="Images/1.png"  />
    </Image.Source>
</Image>

and:

Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
    Dim Trans As RotateTransform = Me.UpArrow.RenderTransform
    Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
    Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub

Related


Code in WPF can't rotate image

Matt I have a situation where I want to rotate a control on a WPF form on a left mouse button down event. I've tried to adapt code I found elsewhere, but I haven't gotten there yet. Here is what I have: XAML: <Image Name="UpArrow"

Can't rotate image in react

kill me I am working on React-Avatar editor. My spin buttons are not working as expected. Here is my code link : https://codesandbox.io/s/example-for-react-avatar-editor-ofoz4 oskenforth You are not using a rotation value. I've fixed it in this fork: https://c

Can't rotate image in react

kill me I am working on React-Avatar editor. My spin buttons are not working as expected. Here is my code link : https://codesandbox.io/s/example-for-react-avatar-editor-ofoz4 oskenforth You are not using a rotation value. I've fixed it in this fork: https://c

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate simple .jpg image

third Some parts of the image captured by my Motorola Moto phone (which is a problem in many models, e.g. Moto One, Moto Z2 Play, etc.) are rotated 90 degrees clockwise. Even though I've rotated hundreds of thousands of images using IrfanView over the past 15

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't see image in Gridview in WPF

Faisal Thayyil I am trying to display an image along with data in a list view data item. I am able to see the text item in the list view but not the image. I bind the image with the image file path. The same image path is in the listview in the same page. But

Can't see image in Gridview in WPF

Faisal Thayyil I am trying to display an image along with data in a list view data item. I am able to see the text item in the list view but not the image. I bind the image with the image file path. The same image path is in the listview in the same page. But

How to rotate Grid background image in WPF?

Sagotharan I want to rotate Grid background image in WPF. I have animation code for image rotation. But when I implement in grid background, Image is not accepted, so Imagebrush is only accepted. <Grid.Background> <ImageBrush ImageSource="../Imag

Rotate image in WPF regardless of screen size

Ruben 2776 I'm currently having issues using RotateTransformand rotating images in WPF LayoutTransform. When the pixel height of the image is greater than the monitor height and rotated 90º or 270º, the window size will be higher than the monitor screen resolu

Rotate image by X degrees C# wpf

username This has been bugging me for years, I just want an easy way to rotate an image by X degrees. (This is for turret defense games where turrets need to shoot in a certain direction) I want something like this: public Image getRotatedImage(Image img, floa

Rotate image by X degrees C# wpf

username This has been bugging me for years, I just want an easy way to rotate an image by X degrees. (This is for turret defense games where turrets need to fire in a specific direction) I want something like this: public Image getRotatedImage(Image img, floa

How to rotate Grid background image in WPF?

Sagotharan I want to rotate Grid background image in WPF. I have animation code for image rotation. But when I implement in grid background, Image is not accepted, so Imagebrush is only accepted. <Grid.Background> <ImageBrush ImageSource="../Imag

Rotate image by X degrees C# wpf

username This has been bugging me for years, I just want an easy way to rotate an image by X degrees. (This is for turret defense games where turrets need to fire in a specific direction) I want something like this: public Image getRotatedImage(Image img, floa

Why doesn't this image rotate?

Turn I want to rotate image on html canvas before drawing. I used the following code ( ctxis canvas context): ctx.rotate(Math.PI / 4); var img = new Image(); img.src = "Test.png"; img.onload = function() { ctx.drawImage(img, 0, 0); } ctx.beginPath(); ctx.r

Why doesn't this image rotate?

Turn I want to rotate image on html canvas before drawing. I used the following code ( ctxis canvas context): ctx.rotate(Math.PI / 4); var img = new Image(); img.src = "Test.png"; img.onload = function() { ctx.drawImage(img, 0, 0); } ctx.beginPath(); ctx.r

Can't rotate rectangle in canvas

Rajib I am new to HTML5 canvas. I'm trying to build a game area using an HTML canvas, where someone can drive around a car inside the Canvas area, like if I press the left arrow key, the car will turn to the left, and if the right arrow key is pressed, the car

WPF can't retrieve WebP image from URL?

Highest I can't retrieve the image from the url. Previously, I couldn't connect to the site at all until the HttpClient header was set. I can retrieve images from other sources but not from this specific source. Code to retrieve the image: var img = new Bitmap

WPF can't retrieve WebP image from URL?

Highest I can't retrieve the image from the url. Previously, I couldn't connect to the site at all until the HttpClient header was set. I can retrieve images from other sources but not from this specific source. Code to retrieve the image: var img = new Bitmap

Can't access WPF storyboard name in code behind

Yahoo! I want to make blinking TextBlock text. However, the Storyboard cannot be accessed. Please see the code below: XAML <Page.Resources> <Storyboard x:Name="BlinkLabelStoryBoard" x:Key="BlinkLabel" Duration="0:0:2" RepeatBehavior="Forever"> <Col