状态栏右下角显示图像像素坐标和灰度

This commit is contained in:
李伟
2026-04-30 15:51:29 +08:00
parent 4d5fa04920
commit 6b35da4cc0
5 changed files with 74 additions and 3 deletions
@@ -41,6 +41,7 @@
MouseLeftButtonDown="Canvas_MouseLeftButtonDown"
MouseLeftButtonUp="Canvas_MouseLeftButtonUp"
MouseMove="Canvas_MouseMove"
MouseLeave="Canvas_MouseLeave"
MouseRightButtonDown="Canvas_MouseRightButtonDown"
PreviewMouseRightButtonUp="Canvas_PreviewMouseRightButtonUp">
@@ -299,6 +299,19 @@ namespace XP.ImageProcessing.RoiControl.Controls
set => SetValue(ShowCrosshairProperty, value);
}
// ── 光标信息(像素坐标 + 灰度值)──
public static readonly DependencyProperty CursorInfoProperty =
DependencyProperty.Register(nameof(CursorInfo), typeof(string), typeof(PolygonRoiCanvas),
new PropertyMetadata("X: -- Y: -- Gray: --"));
/// <summary>鼠标在图像上的像素坐标和灰度值,格式: "X: 123 Y: 456 Gray: 128"</summary>
public string CursorInfo
{
get => (string)GetValue(CursorInfoProperty);
set => SetValue(CursorInfoProperty, value);
}
private Line _crosshairH, _crosshairV;
private static void OnShowCrosshairChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -1739,6 +1752,38 @@ namespace XP.ImageProcessing.RoiControl.Controls
#region Mouse Events
private void UpdateCursorInfo(Point pos)
{
if (ImageSource == null)
{
CursorInfo = "X: -- Y: -- Gray: --";
return;
}
int px = (int)pos.X, py = (int)pos.Y;
string grayText = "--";
if (ImageSource is BitmapSource bmp && px >= 0 && py >= 0 && px < bmp.PixelWidth && py < bmp.PixelHeight)
{
try
{
var pixel = new byte[4];
var converted = new FormatConvertedBitmap(bmp, PixelFormats.Bgra32, null, 0);
converted.CopyPixels(new System.Windows.Int32Rect(px, py, 1, 1), pixel, 4, 0);
int gray = (int)(pixel[2] * 0.299 + pixel[1] * 0.587 + pixel[0] * 0.114);
grayText = gray.ToString();
}
catch { }
}
CursorInfo = $"X: {px} Y: {py} Gray: {grayText}";
}
private void Canvas_MouseLeave(object sender, MouseEventArgs e)
{
CursorInfo = "X: -- Y: -- Gray: --";
}
private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
double oldZoom = ZoomScale;
@@ -1881,6 +1926,9 @@ namespace XP.ImageProcessing.RoiControl.Controls
lastMousePosition = currentPosition;
}
}
// 更新光标信息(像素坐标 + 灰度值)
UpdateCursorInfo(e.GetPosition(mainCanvas));
}
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
@@ -265,6 +265,13 @@ namespace XplorePlane.ViewModels
public string CncStatusMessage => _cncEditorViewModel.StatusMessage;
public bool CncHasExecutionError => _cncEditorViewModel.HasExecutionError;
private string _cursorInfoText = "X: -- Y: -- Gray: --";
public string CursorInfoText
{
get => _cursorInfoText;
set => SetProperty(ref _cursorInfoText, value);
}
private void ShowWindow(Window window, string name)
{
window.Owner = Application.Current.MainWindow;
+1 -1
View File
@@ -580,7 +580,7 @@
FontFamily="Consolas"
FontSize="11"
Foreground="White"
Text="x: 0 y: 0" />
Text="{Binding CursorInfoText}" />
</Grid>
</Border>
</Grid>
@@ -17,13 +17,19 @@ namespace XplorePlane.Views
{
private MainViewModel _mainVm;
private void SetStatus(string msg)
private MainViewModel GetMainVm()
{
if (_mainVm == null)
{
try { _mainVm = ContainerLocator.Current?.Resolve<MainViewModel>(); } catch { }
}
if (_mainVm != null) _mainVm.StatusMessage = msg;
return _mainVm;
}
private void SetStatus(string msg)
{
var vm = GetMainVm();
if (vm != null) vm.StatusMessage = msg;
}
public ViewportPanelView()
@@ -113,6 +119,15 @@ namespace XplorePlane.Views
}, Prism.Events.ThreadOption.UIThread);
}
catch { }
// 光标信息:从 RoiCanvas.CursorInfo 同步到 MainViewModel
var cursorInfoDesc = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(
PolygonRoiCanvas.CursorInfoProperty, typeof(PolygonRoiCanvas));
cursorInfoDesc?.AddValueChanged(RoiCanvas, (s, e) =>
{
var vm = GetMainVm();
if (vm != null) vm.CursorInfoText = RoiCanvas.CursorInfo;
});
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)