improve:插入图片
This commit is contained in:
@@ -96,7 +96,6 @@ namespace Ink_Canvas {
|
||||
InkCanvas.SetLeft(img, info.Left);
|
||||
InkCanvas.SetTop(img, info.Top);
|
||||
inkCanvas.Children.Add(img);
|
||||
CenterAndScaleElement(img);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,10 +28,17 @@ namespace Ink_Canvas
|
||||
string timestamp = "img_" + DateTime.Now.ToString("yyyyMMdd_HH_mm_ss_fff");
|
||||
image.Name = timestamp;
|
||||
|
||||
CenterAndScaleElement(image);
|
||||
// 新缩放逻辑:最大宽高为画布一半,并居中
|
||||
double maxWidth = inkCanvas.ActualWidth / 2;
|
||||
double maxHeight = inkCanvas.ActualHeight / 2;
|
||||
double scaleX = maxWidth / image.Width;
|
||||
double scaleY = maxHeight / image.Height;
|
||||
double scale = Math.Min(1, Math.Min(scaleX, scaleY));
|
||||
image.Width = image.Width * scale;
|
||||
image.Height = image.Height * scale;
|
||||
InkCanvas.SetLeft(image, (inkCanvas.ActualWidth - image.Width) / 2);
|
||||
InkCanvas.SetTop(image, (inkCanvas.ActualHeight - image.Height) / 2);
|
||||
|
||||
InkCanvas.SetLeft(image, 0);
|
||||
InkCanvas.SetTop(image, 0);
|
||||
inkCanvas.Children.Add(image);
|
||||
|
||||
timeMachine.CommitElementInsertHistory(image);
|
||||
@@ -105,8 +112,6 @@ namespace Ink_Canvas
|
||||
|
||||
if (mediaElement != null)
|
||||
{
|
||||
CenterAndScaleElement(mediaElement);
|
||||
|
||||
InkCanvas.SetLeft(mediaElement, 0);
|
||||
InkCanvas.SetTop(mediaElement, 0);
|
||||
inkCanvas.Children.Add(mediaElement);
|
||||
@@ -155,27 +160,5 @@ namespace Ink_Canvas
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void CenterAndScaleElement(FrameworkElement element)
|
||||
{
|
||||
double maxWidth = SystemParameters.PrimaryScreenWidth / 2;
|
||||
double maxHeight = SystemParameters.PrimaryScreenHeight / 2;
|
||||
|
||||
double scaleX = maxWidth / element.Width;
|
||||
double scaleY = maxHeight / element.Height;
|
||||
double scale = Math.Min(scaleX, scaleY);
|
||||
|
||||
TransformGroup transformGroup = new TransformGroup();
|
||||
transformGroup.Children.Add(new ScaleTransform(scale, scale));
|
||||
|
||||
double canvasWidth = inkCanvas.ActualWidth;
|
||||
double canvasHeight = inkCanvas.ActualHeight;
|
||||
double centerX = (canvasWidth - element.Width * scale) / 2;
|
||||
double centerY = (canvasHeight - element.Height * scale) / 2;
|
||||
|
||||
transformGroup.Children.Add(new TranslateTransform(centerX, centerY));
|
||||
|
||||
element.RenderTransform = transformGroup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2004,10 +2004,17 @@ namespace Ink_Canvas {
|
||||
string timestamp = "img_" + DateTime.Now.ToString("yyyyMMdd_HH_mm_ss_fff");
|
||||
image.Name = timestamp;
|
||||
|
||||
CenterAndScaleElement(image);
|
||||
// 新缩放逻辑:最大宽高为画布一半,并居中
|
||||
double maxWidth = inkCanvas.ActualWidth / 2;
|
||||
double maxHeight = inkCanvas.ActualHeight / 2;
|
||||
double scaleX = maxWidth / image.Width;
|
||||
double scaleY = maxHeight / image.Height;
|
||||
double scale = Math.Min(1, Math.Min(scaleX, scaleY));
|
||||
image.Width = image.Width * scale;
|
||||
image.Height = image.Height * scale;
|
||||
InkCanvas.SetLeft(image, (inkCanvas.ActualWidth - image.Width) / 2);
|
||||
InkCanvas.SetTop(image, (inkCanvas.ActualHeight - image.Height) / 2);
|
||||
|
||||
InkCanvas.SetLeft(image, 0);
|
||||
InkCanvas.SetTop(image, 0);
|
||||
inkCanvas.Children.Add(image);
|
||||
|
||||
timeMachine.CommitElementInsertHistory(image);
|
||||
|
||||
Reference in New Issue
Block a user