Skip to content

Commit

Permalink
优化图片打开速度
Browse files Browse the repository at this point in the history
  • Loading branch information
theyangfan committed Mar 18, 2023
1 parent aba9bcc commit 8607d4d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 29 deletions.
92 changes: 69 additions & 23 deletions PictureViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,89 @@ public PictureViewer()
#endregion

#region Public Method
public void SetSource(Word.Selection sel)
/// <summary>
/// Show inline shape.
/// </summary>
/// <param name="sel"></param>
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);
/// <summary>
/// Show floating shape.
/// </summary>
/// <param name="sel"></param>
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;
Expand All @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions ThisAddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ private void Application_WindowBeforeDoubleClick(Word.Selection Sel, ref bool Ca
{
Ribbon ribbon = Globals.Ribbons.GetRibbon<Ribbon>();
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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions WordPictureViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<PublishUrl>publish\</PublishUrl>
<InstallUrl />
<TargetCulture>zh-chs</TargetCulture>
<ApplicationVersion>1.0.0.0</ApplicationVersion>
<AutoIncrementApplicationRevision>false</AutoIncrementApplicationRevision>
<ApplicationVersion>1.0.0.1</ApplicationVersion>
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
<UpdateEnabled>true</UpdateEnabled>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>days</UpdateIntervalUnits>
Expand Down

0 comments on commit 8607d4d

Please sign in to comment.