Fixed perf issues

This commit is contained in:
TheXamlGuy
2024-07-02 23:38:52 +01:00
parent 757d938081
commit 3f9d6bf78f
10 changed files with 86 additions and 66 deletions
+41 -33
View File
@@ -11,43 +11,51 @@ public class ImageResizer :
int targetHeight, int targetHeight,
bool maintainAspectRatio) bool maintainAspectRatio)
{ {
using SKBitmap original = SKBitmap.Decode(stream); Bitmap bitmap = new(stream);
float widthRatio = (float)targetWidth / original.Width; if (bitmap.Size.Width != targetWidth || bitmap.Size.Height != targetHeight)
float heightRatio = (float)targetHeight / original.Height;
float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio);
int newWidth = (int)(original.Width * scale);
int newHeight = (int)(original.Height * scale);
using SKBitmap resized = new(newWidth, newHeight);
using SKCanvas canvas = new(resized);
canvas.Clear(SKColors.Transparent);
canvas.DrawBitmap(original, new SKRect(0, 0, newWidth, newHeight));
SKBitmap cropped;
if (maintainAspectRatio)
{ {
int cropX = (newWidth - targetWidth) / 2; using SKBitmap sKBitmap = SKBitmap.Decode(stream);
int cropY = (newHeight - targetHeight) / 2; SKBitmap? cropped = null;
bitmap.Dispose();
cropped = new SKBitmap(targetWidth, targetHeight); float widthRatio = (float)targetWidth / sKBitmap.Width;
using SKCanvas croppedCanvas = new(cropped); float heightRatio = (float)targetHeight / sKBitmap.Height;
SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight); float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio);
croppedCanvas.Clear(SKColors.Transparent);
croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight)); int newWidth = (int)(sKBitmap.Width * scale);
} int newHeight = (int)(sKBitmap.Height * scale);
else
{ using SKBitmap resized = new(newWidth, newHeight);
cropped = resized; using SKCanvas canvas = new(resized);
canvas.Clear(SKColors.Transparent);
canvas.DrawBitmap(sKBitmap, new SKRect(0, 0, newWidth, newHeight));
if (maintainAspectRatio)
{
int cropX = (newWidth - targetWidth) / 2;
int cropY = (newHeight - targetHeight) / 2;
cropped = new SKBitmap(targetWidth, targetHeight);
using SKCanvas croppedCanvas = new(cropped);
SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight);
croppedCanvas.Clear(SKColors.Transparent);
croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight));
}
else
{
cropped = resized;
}
using SKImage image = SKImage.FromBitmap(cropped);
using MemoryStream outputStream = new();
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
return new Bitmap(outputStream);
} }
using SKImage image = SKImage.FromBitmap(cropped); return bitmap;
using MemoryStream outputStream = new();
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
return new Bitmap(outputStream);
} }
} }
+9
View File
@@ -0,0 +1,9 @@
namespace Toolkit.Foundation;
public record Activation
{
public static ActivationEventArgs<TSender, TValue> As<TSender, TValue>(TValue value) => new(value);
public static ActivationEventArgs<TSender> As<TSender>() =>
new();
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record ActivationBuilder(IActivation Value, object? Key = null);
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public record ActivationEventArgs<TSynchronize, TValue>(TValue? Value = default) :
IActivation;
public record ActivationEventArgs<TSynchronize>() :
IActivation;
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface ISynchronize; public interface IActivation;
+6
View File
@@ -74,6 +74,11 @@ public partial class Observable(IServiceProvider provider,
Disposer.Dispose(this); Disposer.Dispose(this);
} }
public virtual void OnInitialize()
{
}
public virtual void Initialize() public virtual void Initialize()
{ {
if (IsInitialized) if (IsInitialized)
@@ -83,6 +88,7 @@ public partial class Observable(IServiceProvider provider,
IsInitialized = true; IsInitialized = true;
Subscriber.Subscribe(this); Subscriber.Subscribe(this);
OnInitialize();
} }
public void Revert() public void Revert()
+16 -10
View File
@@ -266,7 +266,7 @@ public partial class ObservableCollection<TItem> :
Disposer.Dispose(this); Disposer.Dispose(this);
} }
public void Fetch(Func<SynchronizeExpression> aggregateDelegate, public void Activate(Func<ActivationBuilder> aggregateDelegate,
bool reset = false) bool reset = false)
{ {
if (reset) if (reset)
@@ -274,8 +274,8 @@ public partial class ObservableCollection<TItem> :
Clear(); Clear();
} }
SynchronizeExpression expression = aggregateDelegate.Invoke(); ActivationBuilder expression = aggregateDelegate.Invoke();
Publisher.PublishUI(expression.Value, expression.Key); Publisher.Publish(expression.Value, expression.Key);
} }
public IEnumerator<TItem> GetEnumerator() => public IEnumerator<TItem> GetEnumerator() =>
@@ -415,6 +415,11 @@ public partial class ObservableCollection<TItem> :
IsCompatibleObject(value) ? IsCompatibleObject(value) ?
IndexOf((TItem)value!) : -1; IndexOf((TItem)value!) : -1;
public virtual void OnInitialize()
{
}
public virtual void Initialize() public virtual void Initialize()
{ {
if (IsInitialized) if (IsInitialized)
@@ -423,9 +428,10 @@ public partial class ObservableCollection<TItem> :
} }
IsInitialized = true; IsInitialized = true;
Subscriber.Subscribe(this); Subscriber.Subscribe(this);
Synchronize(); OnInitialize();
Activate();
} }
public TItem Insert<T>(int index = 0, public TItem Insert<T>(int index = 0,
@@ -570,15 +576,15 @@ public partial class ObservableCollection<TItem> :
} }
} }
public void Synchronize(bool reset = false) public void Activate(bool reset = false)
{ {
if (reset) if (reset)
{ {
Clear(); Clear();
} }
SynchronizeExpression expression = BuildAggregateExpression(); ActivationBuilder builder = ActivationBuilder();
Publisher.PublishUI(expression.Value, expression.Key); Publisher.PublishUI(builder.Value, builder.Key);
} }
public void Track<T>(string propertyName, Func<T> getter, Action<T> setter) public void Track<T>(string propertyName, Func<T> getter, Action<T> setter)
@@ -590,8 +596,8 @@ public partial class ObservableCollection<TItem> :
} }
} }
protected virtual SynchronizeExpression BuildAggregateExpression() => protected virtual ActivationBuilder ActivationBuilder() =>
new(new SynchronizeEventArgs<TItem>()); new(new ActivationEventArgs<TItem>());
protected virtual void ClearItems() => protected virtual void ClearItems() =>
collection.Clear(); collection.Clear();
-9
View File
@@ -1,9 +0,0 @@
namespace Toolkit.Foundation;
public record Synchronize
{
public static SynchronizeEventArgs<TValue, TOptions> As<TValue, TOptions>(TOptions options) => new(options);
public static SynchronizeEventArgs<TValue> As<TValue>() =>
new();
}
@@ -1,7 +0,0 @@
namespace Toolkit.Foundation;
public record SynchronizeEventArgs<TSynchronize, TValue>(TValue? Value = default) :
ISynchronize;
public record SynchronizeEventArgs<TSynchronize>() :
ISynchronize;
@@ -1,3 +0,0 @@
namespace Toolkit.Foundation;
public record SynchronizeExpression(ISynchronize Value, object? Key = null);