代码拉取完成,页面将自动刷新
using FieldTools.MediaFindOut.Control;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Input;
namespace FieldTools.MediaFindOut
{
/// <summary>
/// ImgShowWindow.xaml 的交互逻辑
/// </summary>
public partial class ImgShowWindow : Window
{
#region 初始化
/// <summary>
/// 默认构造,自带示例数据
/// </summary>
public ImgShowWindow()
{
PicUrl = PicUrls[0];
PicIndex = 1;
this.DataContext = this;
InitializeComponent();
}
/// <summary>
/// 使用文件路径集合初始化
/// </summary>
/// <param name="urls"></param>
public ImgShowWindow(List<string> urls)
{
PicUrls = urls;
PicUrl = urls[0];
PicIndex = 1;
this.DataContext = this;
InitializeComponent();
}
#endregion
#region 实体
/// <summary>
/// 图片集合
/// </summary>
public List<string> PicUrls { get; set; } = [
Path.GetFullPath("./Assets/Images/001.jpg"),
Path.GetFullPath("./Assets/Images/002.jpg"),
Path.GetFullPath("./Assets/Images/003.jpg"),
Path.GetFullPath("./Assets/Images/004.jpg"),
Path.GetFullPath("./Assets/Images/005.jpg"),
Path.GetFullPath("./Assets/Images/006.gif")
];
/// <summary>
/// 图片序列,记得从1开始算
/// </summary>
public int PicIndex
{
get { return (int)GetValue(PicIndexProperty); }
set
{
if (value < 1) SetValue(PicIndexProperty, PicUrls.Count);
else if (value > PicUrls.Count) SetValue(PicIndexProperty, 1);
else SetValue(PicIndexProperty, value);
}
}
/// <summary>
/// Using a DependencyProperty as the backing store for PicIndex. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty PicIndexProperty =
DependencyProperty.Register("PicIndex", typeof(int), typeof(ImgShowWindow), null);
/// <summary>
/// 图片路径
/// </summary>
public string PicUrl
{
get { return (string)GetValue(PicUrlProperty); }
set { SetValue(PicUrlProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for PicUrl. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty PicUrlProperty =
DependencyProperty.Register("PicUrl", typeof(string), typeof(ImgShowWindow), null);
/// <summary>
/// X方向的缩放变换值
/// </summary>
public double ScaleX
{
get { return (double)GetValue(ScaleXProperty); }
set { SetValue(ScaleXProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ScaleX. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ScaleXProperty =
DependencyProperty.Register("ScaleX", typeof(double), typeof(ImgShowWindow), new PropertyMetadata(1d));
/// <summary>
/// Y方向的缩放变换值
/// </summary>
public double ScaleY
{
get { return (double)GetValue(ScaleYProperty); }
set { SetValue(ScaleYProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ScaleY. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ScaleYProperty =
DependencyProperty.Register("ScaleY", typeof(double), typeof(ImgShowWindow), new PropertyMetadata(1d));
/// <summary>
/// X方向的平移变换值
/// </summary>
public double TranslateX
{
get { return (double)GetValue(TranslateXProperty); }
set { SetValue(TranslateXProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for TranslateX. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty TranslateXProperty =
DependencyProperty.Register("TranslateX", typeof(double), typeof(ImgShowWindow), new PropertyMetadata(0d));
/// <summary>
/// Y方向的平移变换值
/// </summary>
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for TranslateY. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty TranslateYProperty =
DependencyProperty.Register("TranslateY", typeof(double), typeof(ImgShowWindow), new PropertyMetadata(0d));
/// <summary>
/// 最大缩放比例
/// </summary>
private double MaxScale { get; set; } = 10;
/// <summary>
/// 最小缩放比例
/// </summary>
private double MinScale { get; set; } = 0.2;
private double Dx { get; set; } = 0;
private double Dy { get; set; } = 0;
private double OldX { get; set; } = 0;
private double OldY { get; set; } = 0;
#endregion
#region 事件
/// <summary>
/// 向前
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_Left(object sender, RoutedEventArgs e) => PrevOne();
/// <summary>
/// 向后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_Right(object sender, RoutedEventArgs e) => NextOne();
/// <summary>
/// 绑定按键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_KeyDown(object sender, KeyEventArgs e)
{
//左
if (e.KeyStates == Keyboard.GetKeyStates(Key.Left) || e.KeyStates == Keyboard.GetKeyStates(Key.A))
{
PrevOne();
}
//右
else if (e.KeyStates == Keyboard.GetKeyStates(Key.Right) || e.KeyStates == Keyboard.GetKeyStates(Key.D))
{
NextOne();
}
}
/// <summary>
/// 滚轮缩放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
if (ScaleX > MaxScale) ScaleX = 2;
else ScaleX += 0.1;
if (ScaleY > MaxScale) ScaleY = 2;
else ScaleY += 0.1;
}
else if (e.Delta < 0)
{
if (ScaleX < MinScale) ScaleX = 0.2;
else ScaleX -= 0.1;
if (ScaleY < MinScale) ScaleY = 0.2;
else ScaleY -= 0.1;
}
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_SaveAs_Click(object sender, RoutedEventArgs e)
{
var outPath = MainWindow.SelectFolder();
if (File.Exists(PicUrl) && Directory.Exists(outPath))
{
MainWindow.SaveAsFile(PicUrl, outPath);
MessageManage.Success($"另存为成功,输出目录为{outPath}");
//System.Diagnostics.Process.Start(outPath);
}
}
/// <summary>
/// 还原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Restore_Click(object sender, RoutedEventArgs e) => RestoreImgRenderTransform();
/// <summary>
/// 拖动图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Img_PreviewMouseMove(object sender, MouseEventArgs e)
{
var x = e.GetPosition(this).X;
//var y = e.GetPosition(this).Y - CustomWindow.HeaderHeigth.Value;
var y = e.GetPosition(this).Y;
if (e.LeftButton == MouseButtonState.Pressed)
{
//开启Drop可以实现,但处理内容比较多,不方便
//base.OnMouseMove(e);
// Package the data.
//DataObject data = new DataObject();
//data.SetData("Double", x);
//data.SetData("Double", y);
//data.SetData("Object", img);
//// Initiate the drag-and-drop operation.
//DragDrop.DoDragDrop(img, data, DragDropEffects.Copy | DragDropEffects.Move);
//MessageManage.Default($"当前位置({x},{y})");
if (OldX != 0 && OldY != 0)
{
Dx = x - OldX;
Dy = y - OldY;
}
OldX = x;
OldY = y;
TranslateX += Dx;
TranslateY += Dy;
}
//抬起的时候,清除旧数据,可以避免图片漂移
if (e.LeftButton == MouseButtonState.Released)
{
OldX = 0; OldY = 0;
}
}
private void Img_Drop(object sender, DragEventArgs e)
{
}
/// <summary>
/// 复制路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_CopyFilePath_Click(object sender, RoutedEventArgs e) => Clipboard.SetDataObject(PicUrl);
#endregion
#region 方法
/// <summary>
/// 重置图片的变换
/// </summary>
private void RestoreImgRenderTransform()
{
ScaleX = 1d;
ScaleY = 1d;
TranslateX = 0; ;
TranslateY = 0;
OldX = 0;
OldY = 0;
Dx = 0;
Dy = 0;
}
/// <summary>
/// 上一个
/// </summary>
private void PrevOne()
{
RestoreImgRenderTransform();
PicIndex--;
PicUrl = PicUrls[PicIndex - 1];
}
/// <summary>
/// 下一个
/// </summary>
private void NextOne()
{
RestoreImgRenderTransform();
PicIndex++;
PicUrl = PicUrls[PicIndex - 1];
}
#endregion
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。