Skip to content

Commit

Permalink
[opt] BtnImageInsert_Click
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangSakura committed Aug 4, 2024
1 parent 1f83827 commit f295922
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Ink Canvas/MainWindow_cs/MW_ImageControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Ink_Canvas.Helpers;
using Microsoft.Win32;

namespace Ink_Canvas
Expand All @@ -28,6 +30,8 @@ private async void BtnImageInsert_Click(object sender, RoutedEventArgs e)
string timestamp = "img_" + DateTime.Now.ToString("ddHHmmssfff");
image.Name = timestamp;

CenterAndScaleImage(image);

InkCanvas.SetLeft(image, 0);
InkCanvas.SetTop(image, 0);
inkCanvas.Children.Add(image);
Expand All @@ -37,6 +41,28 @@ private async void BtnImageInsert_Click(object sender, RoutedEventArgs e)
}
}

private void CenterAndScaleImage(Image image)
{
double maxWidth = SystemParameters.PrimaryScreenWidth / 2;
double maxHeight = SystemParameters.PrimaryScreenHeight / 2;

double scaleX = maxWidth / image.Width;
double scaleY = maxHeight / image.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 - image.Width * scale) / 2;
double centerY = (canvasHeight - image.Height * scale) / 2;

transformGroup.Children.Add(new TranslateTransform(centerX, centerY));

image.RenderTransform = transformGroup;
}

private async Task<Image> CreateAndCompressImageAsync(byte[] imageBytes)
{
return await Dispatcher.InvokeAsync(() =>
Expand Down

0 comments on commit f295922

Please sign in to comment.