162 lines
5.9 KiB
C#
162 lines
5.9 KiB
C#
using AnQing.Commons;
|
|
using Microsoft.Win32;
|
|
using RGD.DBUtility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace AnQing.View
|
|
{
|
|
/// <summary>
|
|
/// ImageSetUp.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class ImageSetUp : UserControl
|
|
{
|
|
private Point startPoint;
|
|
private bool isDragging = false;
|
|
private string imagePath;
|
|
private double scaleRatio = 1; // 记录缩放比例
|
|
private int originalWidth, originalHeight; // 记录原图尺寸
|
|
|
|
public ImageSetUp(string path)
|
|
{
|
|
InitializeComponent();
|
|
SelectionCanvas.MouseDown += SelectionCanvas_MouseDown;
|
|
SelectionCanvas.MouseMove += SelectionCanvas_MouseMove;
|
|
SelectionCanvas.MouseUp += SelectionCanvas_MouseUp;
|
|
imagePath = path;
|
|
ImageView();
|
|
}
|
|
// 选择图片
|
|
private void SelectImage_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
OpenFileDialog openFileDialog = new OpenFileDialog
|
|
{
|
|
Filter = "图片文件 (*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp"
|
|
};
|
|
|
|
if (openFileDialog.ShowDialog() == true)
|
|
{
|
|
imagePath = openFileDialog.FileName;
|
|
ImageView();
|
|
}
|
|
}
|
|
public void ImageView()
|
|
{
|
|
BitmapImage bitmap = new BitmapImage(new Uri(imagePath));
|
|
|
|
// 记录原始尺寸
|
|
originalWidth = bitmap.PixelWidth;
|
|
originalHeight = bitmap.PixelHeight;
|
|
|
|
// 计算缩放比例,使高度 = 560
|
|
scaleRatio = 560.0 / originalHeight;
|
|
|
|
// 设置缩放后的图片
|
|
int scaledWidth = (int)(originalWidth * scaleRatio);
|
|
int scaledHeight = (int)(originalHeight * scaleRatio);
|
|
|
|
BitmapImage resizedBitmap = new BitmapImage();
|
|
resizedBitmap.BeginInit();
|
|
resizedBitmap.UriSource = new Uri(imagePath);
|
|
resizedBitmap.DecodePixelHeight = scaledHeight; // 按高度缩放
|
|
resizedBitmap.EndInit();
|
|
|
|
MainImage.Source = resizedBitmap;
|
|
SelectionCanvas.Width = scaledWidth;
|
|
SelectionCanvas.Height = scaledHeight;
|
|
}
|
|
// 鼠标按下,开始选择区域
|
|
private void SelectionCanvas_MouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
if (MainImage.Source == null) return;
|
|
|
|
isDragging = true;
|
|
startPoint = e.GetPosition(SelectionCanvas);
|
|
|
|
Canvas.SetLeft(SelectionRectangle, startPoint.X);
|
|
Canvas.SetTop(SelectionRectangle, startPoint.Y);
|
|
SelectionRectangle.Width = 0;
|
|
SelectionRectangle.Height = 0;
|
|
SelectionRectangle.Visibility = Visibility.Visible;
|
|
}
|
|
|
|
// 鼠标移动,动态调整选取框
|
|
private void SelectionCanvas_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (isDragging)
|
|
{
|
|
Point currentPoint = e.GetPosition(SelectionCanvas);
|
|
|
|
double x = Math.Min(currentPoint.X, startPoint.X);
|
|
double y = Math.Min(currentPoint.Y, startPoint.Y);
|
|
double width = Math.Abs(currentPoint.X - startPoint.X);
|
|
double height = Math.Abs(currentPoint.Y - startPoint.Y);
|
|
|
|
Canvas.SetLeft(SelectionRectangle, x);
|
|
Canvas.SetTop(SelectionRectangle, y);
|
|
SelectionRectangle.Width = width;
|
|
SelectionRectangle.Height = height;
|
|
}
|
|
}
|
|
|
|
// 鼠标松开,完成选区
|
|
private void SelectionCanvas_MouseUp(object sender, MouseButtonEventArgs e)
|
|
{
|
|
isDragging = false;
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
// 释放图像或其他资源
|
|
if (MainImage.Source is BitmapImage bitmapImage)
|
|
{
|
|
// 关闭图像流
|
|
bitmapImage.StreamSource?.Close();
|
|
}
|
|
DbHelperSQL.ExecuteSql($"INSERT INTO IMAGEINFO (x, y, width, height,[index]) VALUES ({originalX}, {originalY}, {originalWidthSelected}, {originalHeightSelected},1)");
|
|
Common.main.mianWindowViewModel.GetPossibleRegions();
|
|
Common.MenuViewQuit();
|
|
}
|
|
int originalX, originalY, originalWidthSelected, originalHeightSelected;
|
|
// 获取选区数据(转换为原图尺寸)
|
|
private void GetSelectionData_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (MainImage.Source == null || SelectionRectangle.Visibility == Visibility.Collapsed)
|
|
{
|
|
MessageBox.Show("请先选择图片并框选区域!");
|
|
return;
|
|
}
|
|
|
|
// 获取缩小后的选区坐标
|
|
double scaledX = Canvas.GetLeft(SelectionRectangle);
|
|
double scaledY = Canvas.GetTop(SelectionRectangle);
|
|
double scaledWidth = SelectionRectangle.Width;
|
|
double scaledHeight = SelectionRectangle.Height;
|
|
|
|
// 转换回原图尺寸
|
|
originalX = (int)(scaledX / scaleRatio);
|
|
originalY = (int)(scaledY / scaleRatio);
|
|
originalWidthSelected = (int)(scaledWidth / scaleRatio);
|
|
originalHeightSelected = (int)(scaledHeight / scaleRatio);
|
|
|
|
// 显示选区数据
|
|
string selectionInfo = $"原图坐标: X={originalX}, Y={originalY}, 宽={originalWidthSelected}, 高={originalHeightSelected}";
|
|
InfoText.Text = selectionInfo;
|
|
//MessageBox.Show(selectionInfo);
|
|
}
|
|
|
|
}
|
|
}
|