improve:图片插入

This commit is contained in:
2025-08-31 09:20:35 +08:00
parent b8fe5bbd66
commit 33948c604c
+19 -13
View File
@@ -141,19 +141,6 @@ namespace Ink_Canvas
element.RenderTransform = transformGroup; element.RenderTransform = transformGroup;
} }
// 设置位置
if (position.HasValue)
{
// 在指定位置居中显示
InkCanvas.SetLeft(image, position.Value.X - image.Width / 2);
InkCanvas.SetTop(image, position.Value.Y - image.Height / 2);
}
else
{
// 使用与文件选择相同的居中和缩放逻辑
CenterAndScaleElement(image);
}
// 设置图片属性,避免被InkCanvas选择系统处理 // 设置图片属性,避免被InkCanvas选择系统处理
image.IsHitTestVisible = true; image.IsHitTestVisible = true;
image.Focusable = false; image.Focusable = false;
@@ -171,6 +158,23 @@ namespace Ink_Canvas
// 添加到画布 // 添加到画布
inkCanvas.Children.Add(image); inkCanvas.Children.Add(image);
// 等待图片加载完成后再进行居中处理
image.Loaded += (sender, e) =>
{
// 确保在UI线程中执行
Dispatcher.BeginInvoke(new Action(() =>
{
// 先进行缩放居中处理
CenterAndScaleElement(image);
// 如果有指定位置,调整到指定位置
if (position.HasValue)
{
// 在指定位置居中显示
InkCanvas.SetLeft(image, position.Value.X - image.Width / 2);
InkCanvas.SetTop(image, position.Value.Y - image.Height / 2);
}
// 绑定事件处理器 // 绑定事件处理器
if (image is FrameworkElement elementForEvents) if (image is FrameworkElement elementForEvents)
{ {
@@ -188,6 +192,8 @@ namespace Ink_Canvas
// 设置光标 // 设置光标
elementForEvents.Cursor = Cursors.Hand; elementForEvents.Cursor = Cursors.Hand;
} }
}), System.Windows.Threading.DispatcherPriority.Loaded);
};
// 提交到历史记录 // 提交到历史记录
timeMachine.CommitElementInsertHistory(image); timeMachine.CommitElementInsertHistory(image);