|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
using System.Collections.ObjectModel;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
namespace WpfEditor
|
|
|
{
|
|
|
|
|
|
public partial class RectangleItem : ObservableObject
|
|
|
{
|
|
|
[ObservableProperty]
|
|
|
private int id;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double left;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double top;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double width;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double height;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private bool isVisible = true;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private bool isHovered = false;
|
|
|
|
|
|
public RectangleItem(int id, double left, double top, double width, double height)
|
|
|
{
|
|
|
Id = id;
|
|
|
Left = left;
|
|
|
Top = top;
|
|
|
Width = width;
|
|
|
Height = height;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public partial class ViewModel : ObservableObject
|
|
|
{
|
|
|
[ObservableProperty]
|
|
|
private BitmapSource? image;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private bool isDrawingRectangle = false;
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double rectangleLeft = 0;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double rectangleTop = 0;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double rectangleWidth = 0;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private double rectangleHeight = 0;
|
|
|
|
|
|
[ObservableProperty]
|
|
|
private bool rectangleVisible = false;
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
public ObservableCollection<RectangleItem> rectangles = new ObservableCollection<RectangleItem>();
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
public int rectangleCounter = 0;
|
|
|
|
|
|
public ViewModel()
|
|
|
{
|
|
|
LoadSampleImage();
|
|
|
}
|
|
|
|
|
|
public void LoadImage(string filePath)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
BitmapImage bitmap = new BitmapImage();
|
|
|
bitmap.BeginInit();
|
|
|
bitmap.UriSource = new Uri(filePath);
|
|
|
bitmap.EndInit();
|
|
|
|
|
|
Image = bitmap;
|
|
|
|
|
|
ClearAllRectangles();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
throw new Exception($"无法加载图片: {ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public int AddRectangle(double left, double top, double width, double height)
|
|
|
{
|
|
|
RectangleCounter++;
|
|
|
var newRectangle = new RectangleItem(RectangleCounter, left, top, width, height);
|
|
|
Rectangles.Add(newRectangle);
|
|
|
return RectangleCounter;
|
|
|
}
|
|
|
|
|
|
public int AddRectangle(int Id, double left, double top, double width, double height)
|
|
|
{
|
|
|
var newRectangle = new RectangleItem(Id, left, top, width, height);
|
|
|
Rectangles.Add(newRectangle);
|
|
|
RectangleCounter = Rectangles.Count;
|
|
|
return RectangleCounter;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ClearRectangle()
|
|
|
{
|
|
|
RectangleVisible = false;
|
|
|
IsDrawingRectangle = false;
|
|
|
RectangleLeft = 0;
|
|
|
RectangleTop = 0;
|
|
|
RectangleWidth = 0;
|
|
|
RectangleHeight = 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ClearAllRectangles()
|
|
|
{
|
|
|
Rectangles.Clear();
|
|
|
RectangleCounter = 0;
|
|
|
ClearRectangle();
|
|
|
}
|
|
|
|
|
|
private void LoadSampleImage()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
CreateTestImage();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void CreateTestImage()
|
|
|
{
|
|
|
var bitmap = new WriteableBitmap(800, 600, 96, 96, System.Windows.Media.PixelFormats.Bgr24, null);
|
|
|
|
|
|
var stride = bitmap.PixelWidth * 3;
|
|
|
var pixels = new byte[bitmap.PixelHeight * stride];
|
|
|
|
|
|
for (int y = 0; y < bitmap.PixelHeight; y++)
|
|
|
{
|
|
|
for (int x = 0; x < bitmap.PixelWidth; x++)
|
|
|
{
|
|
|
int index = y * stride + x * 3;
|
|
|
pixels[index] = (byte)(x % 256);
|
|
|
pixels[index + 1] = (byte)(y % 256);
|
|
|
pixels[index + 2] = (byte)((x + y) % 256);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), pixels, stride, 0);
|
|
|
Image = bitmap;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ResetImageDimensions()
|
|
|
{
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public void RemoveRectangle(RectangleItem rectangle)
|
|
|
{
|
|
|
if (rectangle != null && Rectangles.Contains(rectangle))
|
|
|
{
|
|
|
Rectangles.Remove(rectangle);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void RemoveRectangleById(int id)
|
|
|
{
|
|
|
var rectangle = Rectangles.FirstOrDefault(r => r.Id == id);
|
|
|
if (rectangle != null)
|
|
|
{
|
|
|
Rectangles.Remove(rectangle);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |