Witam
Chciałbym obracać obrazek tam gdzie złapie myszką.
Niestety kiedy obrócę obrazek i chcę złapać w innym miejscu to łapie obrazek lecz niestety w innym miejscu niż pokazywałem.
W czym Może być problem, albo jak to poprawić?
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Image image = new Image();
static Point pozycja;
//kontroler
public class Kontroler
{
private static RotateTransform rotuj;
private static TranslateTransform transluj;
private static ScaleTransform skaluj;
private static FrameworkElement lassel = null;
private static Boolean doScal = false;
private static Point P;
private static Double sumuj;
private static Point poprzedni;
private static Boolean first = true;
private static Point star = new Point();
private static TransformGroup tg;
private static Point move = new Point();
//add position
public static void Posadd(Point delta, Point kart, Point mysz)
{
transluj.X += delta.X; //translate with mouse
transluj.Y += delta.Y;
rotuj.Angle += 1;
sumuj += 0.5;
RegEl(lassel);
}
public static void changeCenter( Point kart, Point mysz)
{
Point move=new Point();
move.X = (mysz.X - transluj.X -rotuj.CenterX ) * Math.Cos(rotuj.Angle * Math.PI / 180) ;
move.Y = (mysz.Y - transluj.Y - rotuj.CenterY) * Math.Cos(rotuj.Angle * Math.PI / 180);
rotuj.CenterX += (move.X); //change center rotate <-tu mam problem
rotuj.CenterY += (move.Y) ;
sumuj = 0.0;
}
public static Point gdzie()
{
Point aktualne = new Point();
aktualne.X = transluj.X;
aktualne.Y = transluj.Y;
return aktualne;
}
public static double kat()
{
double zm = new double();
zm=rotuj.Angle;
return zm;
}
public static Point gdziecent()
{
RegEl(lassel);
return new Point(rotuj.CenterX, rotuj.CenterY);
}
public static void BaseTrans(FrameworkElement elem)
{
TransformGroup tg = new TransformGroup();
ScaleTransform sc = new ScaleTransform();
tg.Children.Add(sc);
RotateTransform rt = new RotateTransform();
tg.Children.Add(rt);
TranslateTransform tt = new TranslateTransform();
tg.Children.Add(tt);
elem.RenderTransform = tg;
}
//czyszczenie transformacji
public static void cleanTr()
{
if (lassel != null)
{
lassel.Opacity = 0.75;
lassel = null;
}
rotuj = null;
transluj = null;
skaluj = null;
}
//metoda rejestrujaca
public static void RegEl(UIElement elem)
{
first=false;
cleanTr();
if (elem != null && elem.RenderTransform is TransformGroup)
{
if (elem is FrameworkElement)
{
lassel = elem as FrameworkElement;
lassel.Opacity = 0.75;
}
tg = elem.RenderTransform as TransformGroup;
foreach (Transform tran in tg.Children)
{
if (tran is TranslateTransform)
{
transluj = tran as TranslateTransform;
}
if (tran is RotateTransform)
{
rotuj = tran as RotateTransform;
}
if (tran is ScaleTransform)
{
skaluj = tran as ScaleTransform;
}
}
}
}
public static void PL()
{
if (transluj != null)
transluj.X -= 10;
}
public static void spr(){
if (doScal == true)
{
rotuj.CenterX = lassel.ActualWidth/2;
rotuj.CenterY = lassel.ActualHeight/2;
doScal = false;
}
}
public static void PR()
{
if (transluj != null)
transluj.X += 10;
}
public static void PU()
{
if (transluj != null)
transluj.Y -= 10;
}
public static void PD()
{
if (transluj != null)
transluj.Y += 10;
}
public static void OL()
{
if (rotuj != null)
{
rotuj.CenterX = lassel.ActualWidth / 2;
rotuj.CenterY = lassel.ActualHeight / 2;
rotuj.Angle -= 1;
}
}
public static void OR()
{
if (rotuj != null)
{
rotuj.CenterX = lassel.ActualWidth / 2;
rotuj.CenterY = lassel.ActualHeight / 2;
rotuj.Angle += 1;
}
}
public static void res(double scala)
{
if (skaluj != null)
{
skaluj.ScaleX = scala;
skaluj.ScaleY = scala;
doScal = true;
skaluj.CenterX = lassel.ActualWidth/2;
skaluj.CenterY = lassel.ActualHeight/2;
}
}
}
//koniec klasy
private void add(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
FrameworkElement elem = null;
ofd.Title = "Select a picture";
ofd.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
Image img = new Image();
img.BringIntoView();
if (ofd.ShowDialog() == true)
{
Grid.SetRow(img, 0);
Grid.SetZIndex(img, -2);
}
//if (rd.Next() % 2 == 0)
// {
if (ofd.FileName.Length > 0)
{
img.Source = new BitmapImage(new Uri(ofd.FileName));
//kartka.Children.Add(img);
elem = img;
Kontroler.BaseTrans(elem);
elem.MouseLeftButtonDown += czus;
elem.MouseMove += MoveMouse;
elem.MouseLeftButtonUp += Ediend;
// kartka.ClipToBounds=;
kartka.Children.Add(img);
Canvas.SetZIndex(img, 1);
kartka.ClipToBounds = true;
//crop image
//-- pole=new RectangleGeometry();
//-- pole.Rect = new Rect(0, 0,800,511);
}
}
//wybieranie obrazka
private bool isMouseDown = false;
private void czus(object sender, MouseButtonEventArgs e)
{
Kontroler.RegEl(e.OriginalSource as UIElement);
Kontroler.changeCenter(e.GetPosition(kartka), Mouse.GetPosition(null));
poi.Text = "mouse-" + Mouse.GetPosition(null).X.ToString()+" "+Mouse.GetPosition(null).Y.ToString();
poi.AppendText("gdzie" + Kontroler.gdzie().X + " "+Kontroler.gdzie().Y+" "+ "kat" +Kontroler.kat()%360);
poi.AppendText("\ncent" + Kontroler.gdziecent().X.ToString() + " " + Kontroler.gdziecent().Y.ToString());
isMouseDown = true;
pozycja = e.GetPosition(kartka);
}
//koniec edycji
private void Ediend(object sender, MouseButtonEventArgs e)
{
isMouseDown = false;
}
//ruch myszy
private void MoveMouse(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = e.GetPosition(kartka);
Point delta = new Point(mousePos.X - pozycja.X,
mousePos.Y - pozycja.Y);
Kontroler.Posadd(delta, pozycja, mousePos);
pozycja = mousePos;
}
}
private void przesL(object sender, RoutedEventArgs e)
{
Kontroler.PL();
}
private void przesP(object sender, RoutedEventArgs e)
{
Kontroler.PR();
}
private void przesD(object sender, RoutedEventArgs e)
{
Kontroler.PD();
}
private void przesG(object sender, RoutedEventArgs e)
{
Kontroler.PU();
}
private void RotR(object sender, RoutedEventArgs e)
{
Kontroler.OR();
}
private void RotL(object sender, RoutedEventArgs e)
{
Kontroler.OL();
}
private void ZmRoz(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Kontroler.res(Convert.ToDouble(zmien.Value));
}
}
}
Natomiast xaml wygląda tak
Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="650" Width="800">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="BorderBrush" Value="white"/>
</Style>
</Window.Resources>
<Grid Name="any">
<Grid.RowDefinitions>
<RowDefinition MinHeight="400" Height="511*" Name="one"/>
<RowDefinition Height="100" Name="two" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Width="800" Height="500" BorderThickness="2" BorderBrush="Black">
<Canvas Background="Gray" Name="kartka" ></Canvas>
</Border>
<Button Height="30" Width="100" Margin="0,70,678,0" Grid.Row="1" FontSize="15" Click="add" Name="addphoto" Content="Dodaj zdjęcie">
</Button>
<Button Height="30" Width="30" Margin="150,0,0,70" Grid.Row="1" Click="przesG">
<Button.Background>
<ImageBrush ImageSource="images/up.jpg" />
</Button.Background>
</Button>
<Button Height="30" Width="30" Margin="150,0,0,10" Grid.Row="1" Click="przesD">
<Button.Background>
<ImageBrush ImageSource="images/down.jpg"/>
</Button.Background>
</Button>
<Button Height="30" Width="30" Margin="100,0,0,40" Grid.Row="1" Click="przesL">
<Button.Background>
<ImageBrush ImageSource="images/left.jpg"/>
</Button.Background>
</Button>
<Button Height="30" Width="30" Margin="200,0,0,40" Grid.Row="1" Click="przesP">
<Button.Background>
<ImageBrush ImageSource="images/right.jpg"/>
</Button.Background>
</Button>
<Slider Grid.Row="1" Height="30" Margin="289,0,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Left" Name="zmien" Width="150" ValueChanged="ZmRoz" Minimum="0.01" Maximum="2" Value="1" />
<RepeatButton Height="30" Width="30" Margin="550,35,198,35" Grid.Row="1" Click="RotL">
<RepeatButton.Background>
<ImageBrush ImageSource="images/left.jpg"/>
</RepeatButton.Background>
</RepeatButton>
<RepeatButton Height="30" Width="30" Margin="600,35,148,35" Grid.Row="1" Click="RotR">
<RepeatButton.Background>
<ImageBrush ImageSource="images/right.jpg"/>
</RepeatButton.Background>
</RepeatButton>
<Label Content="Label" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="523,72,0,0" Name="label1" VerticalAlignment="Top" Width="243" />
<TextBox Grid.Row="1" Height="60" HorizontalAlignment="Left" Margin="257,0,0,0" Name="poi" VerticalAlignment="Top" Width="244" />
</Grid>
</Window>