improve:插入图片
This commit is contained in:
@@ -1 +1 @@
|
||||
1.7.4.0
|
||||
1.7.5.0
|
||||
|
||||
@@ -3149,6 +3149,52 @@
|
||||
</ui:SimpleStackPanel>
|
||||
</Viewbox>
|
||||
</Border>
|
||||
|
||||
<!-- 图片选择工具栏 -->
|
||||
<Border Name="BorderImageSelectionControl"
|
||||
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="800,900,0,0"
|
||||
CornerRadius="5" Height="80"
|
||||
Background="{DynamicResource FloatBarBackground}"
|
||||
Visibility="Collapsed"
|
||||
BorderThickness="1" BorderBrush="{DynamicResource FloatBarBorderBrush}">
|
||||
<Viewbox Margin="0,-2.5">
|
||||
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="10" Height="60">
|
||||
<Border Name="BorderImageRotateLeft" Margin="0,2,-9,2" Background="Transparent"
|
||||
CornerRadius="{Binding ElementName=BorderImageSelectionControl, Path=CornerRadius}"
|
||||
Width="40" MouseDown="Border_MouseDown" MouseUp="BorderImageRotateLeft_MouseUp">
|
||||
<ui:SimpleStackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
|
||||
<Image Source="/Resources/Icons-Fluent/ic_fluent_flip_horizontal_24_regular.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality" Height="25" Width="25" />
|
||||
<TextBlock Text="左转" FontSize="10" Foreground="{DynamicResource FloatBarForeground}"
|
||||
HorizontalAlignment="Center" />
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
<Border Name="BorderImageRotateRight" Margin="0,2,-9,2" Background="Transparent"
|
||||
CornerRadius="{Binding ElementName=BorderImageSelectionControl, Path=CornerRadius}"
|
||||
Width="40" MouseDown="Border_MouseDown" MouseUp="BorderImageRotateRight_MouseUp">
|
||||
<ui:SimpleStackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
|
||||
<Image Source="/Resources/Icons-Fluent/ic_fluent_flip_vertical_24_regular.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality" Height="25" Width="25" />
|
||||
<TextBlock Text="右转" FontSize="10" Foreground="{DynamicResource FloatBarForeground}"
|
||||
HorizontalAlignment="Center" />
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
<Border Width="1" Height="45" BorderBrush="{DynamicResource FloatBarForeground}"
|
||||
Background="{DynamicResource FloatBarForeground}" />
|
||||
<Border Name="BorderImageDelete" Margin="-8,0,0,0"
|
||||
CornerRadius="{Binding ElementName=BorderImageSelectionControl, Path=CornerRadius}"
|
||||
Width="40" MouseDown="Border_MouseDown" MouseUp="BorderImageDelete_MouseUp">
|
||||
<ui:SimpleStackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
|
||||
<Image Source="/Resources/Icons-Fluent/ic_fluent_delete_24_regular.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality" Height="25" Width="25" />
|
||||
<TextBlock Margin="0,5,0,0" Text="删除" FontSize="10"
|
||||
Foreground="{DynamicResource FloatBarForeground}"
|
||||
HorizontalAlignment="Center" />
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
</ui:SimpleStackPanel>
|
||||
</Viewbox>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
||||
<Grid Visibility="Collapsed">
|
||||
|
||||
@@ -1475,5 +1475,33 @@ namespace Ink_Canvas {
|
||||
SaveSettingsToFile();
|
||||
ApplyNoFocusMode();
|
||||
}
|
||||
|
||||
#region Image Toolbar Event Handlers
|
||||
|
||||
private void BorderImageRotateLeft_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (selectedUIElement is Image image)
|
||||
{
|
||||
RotateImage(image, -90);
|
||||
}
|
||||
}
|
||||
|
||||
private void BorderImageRotateRight_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (selectedUIElement is Image image)
|
||||
{
|
||||
RotateImage(image, 90);
|
||||
}
|
||||
}
|
||||
|
||||
private void BorderImageDelete_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (selectedUIElement is Image image)
|
||||
{
|
||||
DeleteImage(image);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -171,5 +171,90 @@ namespace Ink_Canvas
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Image Operations
|
||||
|
||||
/// <summary>
|
||||
/// 旋转图片
|
||||
/// </summary>
|
||||
/// <param name="image">要旋转的图片</param>
|
||||
/// <param name="angle">旋转角度(正数为顺时针,负数为逆时针)</param>
|
||||
private void RotateImage(Image image, double angle)
|
||||
{
|
||||
if (image == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
// 获取当前的变换
|
||||
var transformGroup = image.RenderTransform as TransformGroup ?? new TransformGroup();
|
||||
|
||||
// 查找现有的旋转变换
|
||||
RotateTransform rotateTransform = null;
|
||||
foreach (Transform transform in transformGroup.Children)
|
||||
{
|
||||
if (transform is RotateTransform rt)
|
||||
{
|
||||
rotateTransform = rt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有旋转变换,创建一个新的
|
||||
if (rotateTransform == null)
|
||||
{
|
||||
rotateTransform = new RotateTransform();
|
||||
transformGroup.Children.Add(rotateTransform);
|
||||
}
|
||||
|
||||
// 设置旋转中心为图片中心
|
||||
rotateTransform.CenterX = image.ActualWidth / 2;
|
||||
rotateTransform.CenterY = image.ActualHeight / 2;
|
||||
|
||||
// 累加旋转角度
|
||||
rotateTransform.Angle = (rotateTransform.Angle + angle) % 360;
|
||||
|
||||
// 应用变换
|
||||
image.RenderTransform = transformGroup;
|
||||
|
||||
// 提交到时间机器以支持撤销
|
||||
// 注意:旋转操作目前不支持撤销,因为需要更复杂的历史记录机制
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录错误但不中断程序
|
||||
System.Diagnostics.Debug.WriteLine($"旋转图片时发生错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除图片
|
||||
/// </summary>
|
||||
/// <param name="image">要删除的图片</param>
|
||||
private void DeleteImage(Image image)
|
||||
{
|
||||
if (image == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
// 从画布中移除图片
|
||||
if (inkCanvas.Children.Contains(image))
|
||||
{
|
||||
inkCanvas.Children.Remove(image);
|
||||
|
||||
// 取消选择
|
||||
DeselectUIElement();
|
||||
|
||||
// 提交到时间机器以支持撤销
|
||||
timeMachine.CommitElementRemoveHistory(image);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录错误但不中断程序
|
||||
System.Diagnostics.Debug.WriteLine($"删除图片时发生错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,6 +467,11 @@ namespace Ink_Canvas {
|
||||
private Point resizeStartPoint;
|
||||
private Rect originalElementBounds;
|
||||
|
||||
// 图片工具栏相关
|
||||
private Border borderImageSelectionControl;
|
||||
private double BorderImageSelectionControlWidth = 200.0;
|
||||
private double BorderImageSelectionControlHeight = 80.0;
|
||||
|
||||
private enum ResizeDirection
|
||||
{
|
||||
None,
|
||||
@@ -501,6 +506,12 @@ namespace Ink_Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化图片工具栏引用
|
||||
if (borderImageSelectionControl == null)
|
||||
{
|
||||
borderImageSelectionControl = FindName("BorderImageSelectionControl") as Border;
|
||||
}
|
||||
|
||||
// 创建8个拖拽手柄
|
||||
CreateResizeHandles();
|
||||
}
|
||||
@@ -585,8 +596,16 @@ namespace Ink_Canvas {
|
||||
InitializeUIElementSelection();
|
||||
}
|
||||
|
||||
// 显示拖拽手柄
|
||||
ShowResizeHandles();
|
||||
// 根据元素类型显示不同的工具栏
|
||||
if (element is Image)
|
||||
{
|
||||
ShowImageToolbar();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 对于其他UI元素,显示拖拽手柄
|
||||
ShowResizeHandles();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -594,6 +613,7 @@ namespace Ink_Canvas {
|
||||
{
|
||||
selectedUIElement = null;
|
||||
HideResizeHandles();
|
||||
HideImageToolbar();
|
||||
}
|
||||
|
||||
private void ShowResizeHandles()
|
||||
@@ -613,6 +633,42 @@ namespace Ink_Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowImageToolbar()
|
||||
{
|
||||
if (selectedUIElement == null || borderImageSelectionControl == null) return;
|
||||
|
||||
var bounds = GetUIElementBounds(selectedUIElement);
|
||||
UpdateImageToolbarPosition(bounds);
|
||||
borderImageSelectionControl.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
private void HideImageToolbar()
|
||||
{
|
||||
if (borderImageSelectionControl != null)
|
||||
{
|
||||
borderImageSelectionControl.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateImageToolbarPosition(Rect bounds)
|
||||
{
|
||||
if (borderImageSelectionControl == null) return;
|
||||
|
||||
// 计算工具栏位置,类似于墨迹选择工具栏的逻辑
|
||||
var toolbarX = bounds.X + bounds.Width / 2 - BorderImageSelectionControlWidth / 2;
|
||||
var toolbarY = bounds.Y + bounds.Height + 10; // 在图片下方10像素处
|
||||
|
||||
// 确保工具栏不会超出画布边界
|
||||
if (toolbarX < 0) toolbarX = 0;
|
||||
if (toolbarX + BorderImageSelectionControlWidth > inkCanvas.ActualWidth)
|
||||
toolbarX = inkCanvas.ActualWidth - BorderImageSelectionControlWidth;
|
||||
|
||||
if (toolbarY + BorderImageSelectionControlHeight > inkCanvas.ActualHeight)
|
||||
toolbarY = bounds.Y - BorderImageSelectionControlHeight - 10; // 如果下方空间不够,显示在上方
|
||||
|
||||
borderImageSelectionControl.Margin = new Thickness(toolbarX, toolbarY, 0, 0);
|
||||
}
|
||||
|
||||
private Rect GetUIElementBounds(UIElement element)
|
||||
{
|
||||
var left = InkCanvas.GetLeft(element);
|
||||
@@ -824,6 +880,8 @@ namespace Ink_Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user