我有两个图像控件,第一个显示原始图像,因此用户可以在单击图像并在其上移动时选择要裁剪的图像的一部分。选择完成后,用户可以通过单击发送按钮将第一个图像的选定部分传递给第二个图像控件。第二个图像控件显示所选部件。
问题是第二个图像上显示的部分是否与实际选定的区域不匹配。有人可以帮我解决这个问题吗?
我的Xaml代码:
<Window x:Class="testeImgCrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cortar" Height="466" Width="655">
<Grid>
<Image Height="172" HorizontalAlignment="Right" Margin="0,12,24,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="142" />
<Grid x:Name="original" Height="415" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343">
<Image Margin="0,0,0,0" Height="415" Name="image1" Stretch="Fill" Width="342" MouseLeftButtonDown="image1_MouseLeftButtonDown" MouseMove="image1_MouseMove" MouseLeftButtonUp="image1_MouseLeftButtonUp" />
<Canvas Margin="0,0,0,0" x:Name="BackPanel" Height="415" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343">
<Rectangle x:Name="selectionRectangle" Stroke="Red" Fill="Transparent" Visibility="Collapsed" StrokeThickness="2" />
</Canvas>
</Grid>
<Button Content="Open" Height="23" HorizontalAlignment="Left" Margin="362,59,0,0" Name="btnOpen" VerticalAlignment="Top" Width="75" Click="btnOpen_Click" />
<Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="362,88,0,0" Name="btnSend" VerticalAlignment="Top" Width="75" Click="btnSend_Click" IsEnabled="False"/>
</Grid>
C#代码
private bool isDragging = false;
private Point anchorPoint = new Point();
private Boolean nothing = true;
public MainWindow()
{
InitializeComponent();
original.MouseLeftButtonDown += new MouseButtonEventHandler(image1_MouseLeftButtonDown);
original.MouseMove += new MouseEventHandler(image1_MouseMove);
original.MouseLeftButtonUp += new MouseButtonEventHandler(image1_MouseLeftButtonUp);
}
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
op.Title = "Selecione uma imagem!";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
//Receber Documento
image1.Source = new BitmapImage(new Uri(op.FileName));
original.Cursor = Cursors.Cross;
nothing = false;
}
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (nothing == false)
{
if (isDragging == false)
{
anchorPoint.X = e.GetPosition(BackPanel).X;
anchorPoint.Y = e.GetPosition(BackPanel).Y;
isDragging = true;
}
}
}
private void image1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
double x = e.GetPosition(BackPanel).X;
double y = e.GetPosition(BackPanel).Y;
selectionRectangle.SetValue(Canvas.LeftProperty, Math.Min(x, anchorPoint.X));
selectionRectangle.SetValue(Canvas.TopProperty, Math.Min(y, anchorPoint.Y));
selectionRectangle.Width = Math.Abs(x - anchorPoint.X);
selectionRectangle.Height = Math.Abs(y - anchorPoint.Y);
if (selectionRectangle.Visibility != Visibility.Visible)
selectionRectangle.Visibility = Visibility.Visible;
}
}
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragging)
{
isDragging = false;
if (selectionRectangle.Width > 0)
{
btnSend.IsEnable = true;
}
if (selectionRectangle.Visibility != Visibility.Visible)
selectionRectangle.Visibility = Visibility.Visible;
}
}
private void send(Image impressao)
{
if (nothing == false)
{
var imagePosition = image1.TransformToAncestor(image1).Transform(new Point(0, 0));
Rect rect1 = new Rect(Canvas.GetLeft(selectionRectangle), Canvas.GetTop(selectionRectangle), selectionRectangle.Width, selectionRectangle.Height);
System.Windows.Int32Rect rcFrom = new System.Windows.Int32Rect();
rcFrom.X = (int)((rect1.X) * (image1.Source.Width) / (image1.Width));
rcFrom.Y = (int)((rect1.Y) * (image1.Source.Height) / (image1.Height));
rcFrom.Width = (int)((rect1.Width) * (image1.Source.Width) / (image1.Width));
rcFrom.Height = (int)((rect1.Height) * (image1.Source.Height) / (image1.Height));
BitmapSource bs = new CroppedBitmap(image1.Source as BitmapSource, rcFrom);
image2.Source = bs;
}
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
send(image1);
}
}