File size: 5,788 Bytes
21952dc 3db9552 21952dc a282b13 21952dc a282b13 21952dc f5f13be 21952dc f5f13be 21952dc cbf03e6 21952dc 46859da 21952dc e023998 21952dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
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
{
//Image = new BitmapImage(new Uri("pack://application:,,,/sample.jpg"));
}
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;
}
// 在ViewModel类中添加
public void ResetImageDimensions()
{
// 当加载新图片时调用,重置尺寸记录
// 这个方法需要在MainWindow中调用
}
// 删除指定的矩形框
public void RemoveRectangle(RectangleItem rectangle)
{
if (rectangle != null && Rectangles.Contains(rectangle))
{
Rectangles.Remove(rectangle);
}
}
// 根据ID删除矩形框
public void RemoveRectangleById(int id)
{
var rectangle = Rectangles.FirstOrDefault(r => r.Id == id);
if (rectangle != null)
{
Rectangles.Remove(rectangle);
}
}
}
} |