From 8607d4d974bdd81600e030e9a3686814f724010b Mon Sep 17 00:00:00 2001 From: yangfan Date: Sat, 18 Mar 2023 16:47:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E6=89=93?= =?UTF-8?q?=E5=BC=80=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PictureViewer.xaml.cs | 92 ++++++++++++++++++++++++++++++---------- ThisAddIn.cs | 14 ++++-- WordPictureViewer.csproj | 4 +- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/PictureViewer.xaml.cs b/PictureViewer.xaml.cs index 6b1b80f..459f438 100644 --- a/PictureViewer.xaml.cs +++ b/PictureViewer.xaml.cs @@ -48,43 +48,89 @@ public PictureViewer() #endregion #region Public Method - public void SetSource(Word.Selection sel) + /// + /// Show inline shape. + /// + /// + public void ShowInlineShapeSelection(Word.Selection sel) { try { - var type = sel.Type; - // get original shape - var shape = sel.ShapeRange?[1]; - var left = shape.Left; - var top = shape.Top; - var width = shape.Width; - var height = shape.Height; - var lr = shape.LeftRelative; - var tr = shape.TopRelative; - var wr = shape.WidthRelative; - var hr = shape.HeightRelative; - //var shape = sel.InlineShapes?[1]; - + var shape = sel.InlineShapes[1]; // get the bytes of the shape var bits = (byte[])sel.EnhMetaFileBits; using (MemoryStream stream = new MemoryStream(bits)) { Bitmap bitmap = new Bitmap(stream); - // get the actual width + // get the available width double initW = bitmap.Height * (shape.Width / shape.Height); double initH = bitmap.Height; - double x = 0; - if(shape.Left > 0) + // crop the available area + Bitmap cropBmp = Crop(bitmap, 0, 0, (int)initW, (int)initH); + double ratio = (double)initW / initH; + double screenW = SystemParameters.PrimaryScreenWidth; + double screenH = SystemParameters.PrimaryScreenHeight; + if (initW > screenW) { - x = shape.Left * bitmap.Height / shape.Height; + initW = screenW; + initH = initW / ratio; + } + if (initH > screenH) + { + initH = screenH; + initW = initH * ratio; } - double y = 0; - if(shape.Top > 0) + initW *= 0.8; + initH *= 0.8; + UIImage.Width = initW; + UIImage.Height = initH; + + // set image source + using (MemoryStream ms = new MemoryStream()) { - y = shape.Top * bitmap.Height / shape.Height; + cropBmp.Save(ms, ImageFormat.Png); + byte[] bytes = ms.GetBuffer(); + BitmapImage bi = new BitmapImage(); + bi.BeginInit(); + bi.StreamSource = new MemoryStream(bytes); + bi.EndInit(); + UIImage.Source = bi; } + // loading animation + double centerX = initW / 2; + double centerY = initH / 2; + DoubleAnimation aniScale = new DoubleAnimation() { Duration = TimeSpan.FromMilliseconds(250) }; + DoubleAnimation aniX = new DoubleAnimation(centerX, centerX, TimeSpan.FromMilliseconds(0)); + DoubleAnimation aniY = new DoubleAnimation(centerY, centerY, TimeSpan.FromMilliseconds(0)); + aniScale.From = 0; + aniScale.To = 1; + UIScale.BeginAnimation(ScaleTransform.ScaleXProperty, aniScale, HandoffBehavior.Compose); + UIScale.BeginAnimation(ScaleTransform.ScaleYProperty, aniScale, HandoffBehavior.Compose); + UIScale.BeginAnimation(ScaleTransform.CenterXProperty, aniX); + UIScale.BeginAnimation(ScaleTransform.CenterYProperty, aniY); + } + } + catch (Exception e) + { + MessageBox.Show($"{e.Message}\n{e.StackTrace}"); + } + } - Bitmap cropBmp = Crop(bitmap, (int)x, (int)y, (int)initW, (int)initH); + /// + /// Show floating shape. + /// + /// + public void ShowFloatingShapeSelection(Word.Selection sel) + { + try + { + // get the bytes of the shape + var bits = (byte[])sel.EnhMetaFileBits; + using (MemoryStream stream = new MemoryStream(bits)) + { + Bitmap bitmap = new Bitmap(stream); + double initW = bitmap.Width; + double initH = bitmap.Height; double ratio = (double)initW / initH; double screenW = SystemParameters.PrimaryScreenWidth; double screenH = SystemParameters.PrimaryScreenHeight; @@ -106,7 +152,7 @@ public void SetSource(Word.Selection sel) // Set image source using (MemoryStream ms = new MemoryStream()) { - bitmap.Save(ms, ImageFormat.Bmp); + bitmap.Save(ms, ImageFormat.Png); byte[] bytes = ms.GetBuffer(); BitmapImage bi = new BitmapImage(); bi.BeginInit(); diff --git a/ThisAddIn.cs b/ThisAddIn.cs index 0124f3c..bd59c36 100644 --- a/ThisAddIn.cs +++ b/ThisAddIn.cs @@ -25,13 +25,19 @@ private void Application_WindowBeforeDoubleClick(Word.Selection Sel, ref bool Ca { Ribbon ribbon = Globals.Ribbons.GetRibbon(); if (!ribbon.IsEnable) return; - // Filter shapes - if(Sel.Type == Word.WdSelectionType.wdSelectionShape - || Sel.Type == Word.WdSelectionType.wdSelectionInlineShape) + // inline shapes + if(Sel.Type == Word.WdSelectionType.wdSelectionInlineShape) { PictureViewer viewer = new PictureViewer(); viewer.Show(); - viewer.SetSource(Sel); + viewer.ShowInlineShapeSelection(Sel); + } + // floating shapes + else if(Sel.Type == Word.WdSelectionType.wdSelectionShape) + { + PictureViewer viewer = new PictureViewer(); + viewer.Show(); + viewer.ShowFloatingShapeSelection(Sel); } } diff --git a/WordPictureViewer.csproj b/WordPictureViewer.csproj index 1f53110..2a8572d 100644 --- a/WordPictureViewer.csproj +++ b/WordPictureViewer.csproj @@ -33,8 +33,8 @@ publish\ zh-chs - 1.0.0.0 - false + 1.0.0.1 + true true 7 days