0

I have included images on a canvas on my WPF user control:

<Canvas>
 <Thumb Canvas.Left="19" Canvas.Top="-7" Canvas.ZIndex="99"  DragDelta="Thumb_DragDelta">
 <Thumb.Template>
       <ControlTemplate>
           <Image Name="Image1" Source="/Images/Image1.png" Height="120" Width="242" Margin="63,180,696,393" />
        </ControlTemplate>
 </Thumb.Template>
 </Thumb>
 <Thumb Canvas.Left="52" Canvas.Top="6" DragDelta="Thumb_DragDelta">
 <Thumb.Template>
        <ControlTemplate>
         <Image Name="Image2" Source="/Images/Image2.png" Height="120" Width="205" Margin="760,184,74,397" />
        </ControlTemplate>
  </Thumb.Template>
  </Thumb>
  <Thumb Canvas.Left="21" DragDelta="Thumb_DragDelta" Canvas.Top="-18">
  <Thumb.Template>
        <ControlTemplate>
             <Image Name="Image3" Source="/Images/Image3.png" Height="124" Width="260" Margin="82,426,697,151" />
        </ControlTemplate>
   </Thumb.Template>
   </Thumb>
   <Thumb Canvas.Left="30" Canvas.Top="1" DragDelta="Thumb_DragDelta">
   <Thumb.Template>
        <ControlTemplate>
        <Image Name="Image4" Source="/Images/Image4.png" Height="124" Width="255" Margin="744,341,39,236" />
        </ControlTemplate>
   </Thumb.Template>
   </Thumb>
</Canvas>

I have then included the following in the code behind, so that these thumb images can be moved around on screen, which works fine.

// Move images on canvas
    private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        UIElement thumb = e.Source as UIElement;

        Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
        Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
    }

Does anyone know a quick add where I can add to this code with the thumbs on the canvas so that I have the option to rotate these images on a button click in different directions and different angles?

Thanks

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
HHH
  • 3
  • 1
  • 4
  • Maybe this post will get you going: http://stackoverflow.com/questions/7228373/how-to-make-a-image-swivel-in-wpf. – Tor Langlo Apr 30 '13 at 14:26

2 Answers2

2

You may assign a RotateTransform to the RenderTransform property of your Thumbs and modify the Angle of the RotateTransform in an event handler:

<Thumb Canvas.Left="52" Canvas.Top="7" RenderTransformOrigin="0.5,0.5"
       DragDelta="Thumb_DragDelta" MouseDoubleClick="Thumb_MouseDoubleClick">
    <Thumb.RenderTransform>
        <RotateTransform/>
    </Thumb.RenderTransform>
    <Thumb.Template>
        ...
    </Thumb.Template>
</Thumb>

The event handler may look like this:

private void Thumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var thumb = e.Source as UIElement;
    var transform = thumb.RenderTransform as RotateTransform;
    transform.Angle += 90;
}

You should however change the large Margin values on your Thumbs, as they affect the rotation center.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thank you for your suggestion Clemens, that rotates the thumb around the center of my user control, do you know how I can edit this to rotate around the centre of the image itself. Thanks for your help so far :) – HHH May 02 '13 at 19:41
  • 1
    That's what I meant with changing the large Margins. You may however remove the RenderTransformOrigin and play around with the RotateTransform's CenterX and CenterY properties. – Clemens May 02 '13 at 21:03
  • Ok no problem I get you now. Thank you for your help :) – HHH May 03 '13 at 09:21
-1
  Canvas container = new Canvas();
  container.Children.Add(image);
  container.Arrange(new Rect(0, 0, source.PixelWidth, source.PixelHeight));

  // render the result to a new bitmap. 
  RenderTargetBitmap target = new RenderTargetBitmap(targetWidth, targetHeight,       sourceDpiX, sourceDpiY, PixelFormats.Default);
  target.Render(container);

RenderTargetBitmap can be use it in place of a BitmapSource in any control.

Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23