using Bro.Common.ImageCanvas.Shapes;
|
using System.Collections.ObjectModel;
|
using System.Drawing;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Windows;
|
using System.Windows.Forms;
|
using System.Windows.Input;
|
|
namespace Bro.Common.ImageCanvas
|
{
|
/// <summary>
|
/// ImageCanvas.xaml 的交互逻辑
|
/// </summary>
|
public partial class ImageCanvas : System.Windows.Controls.UserControl
|
{
|
bool isFirstLoad = true;
|
|
public ImageCanvas()
|
{
|
InitializeComponent();
|
GotFocus += (sender, args) => Keyboard.Focus(ZoomAndPanControl);
|
|
Loaded += (s, e) => { isFirstLoad = true; };
|
|
_infoClearTimer = new System.Threading.Timer(ClearInfo, null, Timeout.Infinite, Timeout.Infinite);
|
|
ZoomAndPanControl.OnMousePositionChanged += OnMousePointChanged;
|
}
|
|
#region ImageSource
|
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(Bitmap), typeof(ImageCanvas), new PropertyMetadata(new Bitmap(1000, 1000), OnImagSourcePropertyChanged));
|
|
public Bitmap ImageSource
|
{
|
get => GetValue(ImageSourceProperty) as Bitmap;
|
set => SetValue(ImageSourceProperty, value);
|
}
|
|
private static void OnImagSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
ImageCanvas ctrl = d as ImageCanvas;
|
if (ctrl != null)
|
{
|
Bitmap map = e.NewValue as Bitmap;
|
if (map != null)
|
{
|
ctrl.actualContent.Source = map.BitmapToWriteableBitmap();
|
|
if (ctrl.isFirstLoad)
|
{
|
ctrl.isFirstLoad = false;
|
|
Task.Run(() =>
|
{
|
Thread.Sleep(100);
|
ctrl.Dispatcher.Invoke(() =>
|
{
|
ctrl.ZoomAndPanControl.FitCommand?.Execute(null);
|
});
|
});
|
}
|
}
|
}
|
}
|
#endregion
|
|
#region ToolBar
|
public static readonly DependencyProperty IsShowToolBarProperty = DependencyProperty.Register("IsShowToolBar", typeof(bool), typeof(ImageCanvas), new PropertyMetadata(true, OnIsShowToolBarPropertyChanged));
|
|
private static void OnIsShowToolBarPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
ImageCanvas ctrl = d as ImageCanvas;
|
if (ctrl != null)
|
{
|
bool isVisible = (bool)e.NewValue;
|
ctrl.toolBarTray.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
|
}
|
}
|
|
public bool IsShowToolBar
|
{
|
get => (bool)GetValue(IsShowToolBarProperty);
|
set => SetValue(IsShowToolBarProperty, value);
|
}
|
|
private void btnOpenFile_Click(object sender, RoutedEventArgs e)
|
{
|
OpenFileDialog ofd = new OpenFileDialog();
|
ofd.Multiselect = false;
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
{
|
Bitmap map = System.Drawing.Image.FromFile(ofd.FileName) as Bitmap;
|
if (map != null)
|
{
|
ImageSource = map;
|
}
|
}
|
}
|
|
private void btnSaveImage_Click(object sender, RoutedEventArgs e)
|
{
|
if (ImageSource == null)
|
return;
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
sfd.Filter = "JPG文件|*.jpg|BMP文件|*.bmp";
|
if (sfd.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = sfd.FileName;
|
if (filePath.EndsWith("bmp"))
|
{
|
ImageSource.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp);
|
}
|
else
|
{
|
ImageSource.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
|
}
|
|
SetTxtInfo($"图片保存成功! {filePath}");
|
}
|
}
|
|
private void btnSaveImageResults_Click(object sender, RoutedEventArgs e)
|
{
|
|
}
|
|
private void btnFitScreen_Click(object sender, RoutedEventArgs e)
|
{
|
ZoomAndPanControl.FitCommand?.Execute(null);
|
}
|
|
private void btnOriginImage_Click(object sender, RoutedEventArgs e)
|
{
|
ZoomAndPanControl.FillCommand?.Execute(null);
|
}
|
#endregion
|
|
#region Visula Shapes
|
public static readonly DependencyProperty ShapeListProperty =
|
DependencyProperty.Register("ShapeList", typeof(ObservableCollection<ShapeElementBase>), typeof(ImageCanvas), new PropertyMetadata(new ObservableCollection<ShapeElementBase>(), ObservableCollectionChangedCallback));
|
|
private static void ObservableCollectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
var canvas = (ImageCanvas)d;
|
//canvas.visualCanvas.VisualList = new ObservableCollection<ShapeElementBase>(canvas.ShapeList);
|
//canvas.ShapeList.CollectionChanged += canvas.Shapes_CollectionChanged;
|
|
canvas.visualCanvas.VisualList = canvas.ShapeList;
|
}
|
|
public ObservableCollection<ShapeElementBase> ShapeList
|
{
|
get => GetValue(ShapeListProperty) as ObservableCollection<ShapeElementBase>;
|
set
|
{
|
SetValue(ShapeListProperty, value);
|
|
//value.CollectionChanged -= Shapes_CollectionChanged;
|
//value.CollectionChanged += Shapes_CollectionChanged;
|
}
|
}
|
|
//private void Shapes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
//{
|
// //this.visualCanvas.VisualList = new ObservableCollection<ShapeElementBase>(this.ShapeList);
|
//}
|
#endregion
|
|
#region txtInfo
|
readonly System.Threading.Timer _infoClearTimer = null;
|
|
private void ClearInfo(object state)
|
{
|
txtInfo.Dispatcher.Invoke(() =>
|
{
|
txtInfo.Text = "";
|
});
|
}
|
|
private void SetTxtInfo(string info)
|
{
|
txtInfo.Text = info;
|
|
if (!string.IsNullOrWhiteSpace(info))
|
{
|
_infoClearTimer.Change(5000, Timeout.Infinite);
|
}
|
}
|
#endregion
|
|
#region MousePoint
|
private void OnMousePointChanged(System.Windows.Point point)
|
{
|
visualCanvas.OnMousePointChanged(point);
|
}
|
#endregion
|
}
|
}
|