Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
Expand Down Expand Up @@ -432,6 +432,12 @@ public override void OnApplyTemplate()
var t = new RotateTransform();
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this });
_imageMain.LayoutTransform = t;
//在选项卡中可能会加载失败,这里渲染后在加载一次
if (_imageMain.Source == null && ImageSource != null)
{
_imageMain.Source = ImageSource;
Init();
}
}

if (_canvasSmallImg != null)
Expand Down Expand Up @@ -584,7 +590,7 @@ private void ButtonSave_OnClick(object sender, RoutedEventArgs e)
var path = SaveFileDialog.FileName;
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
encoder.Frames.Add(ImageSource);
encoder.Save(fileStream);
}
}
Expand Down Expand Up @@ -983,7 +989,8 @@ static BitmapFrame GetBitmapFrame(Uri source)
{
try
{
return BitmapFrame.Create(source);
//将图片在加到内存中,这样子外部软件,程序都可以读取,删除这个文件
return BitmapFrame.Create(source, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
}
catch
{
Expand Down Expand Up @@ -1012,4 +1019,16 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// 加载图片(可以避免在某些情况下加载失败的情况)
/// </summary>
/// <param name="uri">地址</param>
public void LoadImage(Uri uri)
{
Uri = uri;
ImageSource = BitmapFrame.Create(uri, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
if (_imageMain != null)//在继承等某些情况下肯定会失败,直接使用属性直接赋值才有用
_imageMain.Source = ImageSource;
}
}