|
|
@@ -1,5 +1,6 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
using System.IO;
|
|
|
using QuestPDF.Drawing.Exceptions;
|
|
|
using QuestPDF.Helpers;
|
|
|
@@ -17,31 +18,23 @@ namespace QuestPDF.Infrastructure
|
|
|
{
|
|
|
internal SKImage SkImage { get; }
|
|
|
internal ImageSize Size { get; }
|
|
|
- internal bool IsDocumentScoped { get; }
|
|
|
- internal bool IsDisposed { get; private set; }
|
|
|
-
|
|
|
- internal LinkedList<(GetImageVersionRequest request, SKImage image)> ScaledImageCache { get; } = new();
|
|
|
|
|
|
- internal Image(SKImage image, bool isDocumentScoped)
|
|
|
+ internal LinkedList<(GetImageVersionRequest request, SKImage image)> ScaledImageCache { get; } = new();
|
|
|
+
|
|
|
+ internal Image(SKImage image)
|
|
|
{
|
|
|
SkImage = image;
|
|
|
- IsDocumentScoped = isDocumentScoped;
|
|
|
Size = new ImageSize(image.Width, image.Height);
|
|
|
}
|
|
|
|
|
|
- internal void Dispose()
|
|
|
+ ~Image()
|
|
|
{
|
|
|
- if (IsDisposed)
|
|
|
- return;
|
|
|
-
|
|
|
SkImage.Dispose();
|
|
|
-
|
|
|
+
|
|
|
foreach (var cacheKey in ScaledImageCache)
|
|
|
cacheKey.image.Dispose();
|
|
|
-
|
|
|
- IsDisposed = true;
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
#region Scaling Image
|
|
|
|
|
|
internal SKImage GetVersionOfSize(GetImageVersionRequest request)
|
|
|
@@ -86,49 +79,7 @@ namespace QuestPDF.Infrastructure
|
|
|
if (image == null)
|
|
|
throw new DocumentComposeException("Cannot load or decode provided image.");
|
|
|
|
|
|
- return new Image(image, true);
|
|
|
- }
|
|
|
-
|
|
|
- #endregion
|
|
|
- }
|
|
|
-
|
|
|
- public class SharedImage : Image, IDisposable
|
|
|
- {
|
|
|
- internal SharedImage(SKImage image) : base(image, false)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void Dispose()
|
|
|
- {
|
|
|
- base.Dispose();
|
|
|
- }
|
|
|
-
|
|
|
- #region public constructors
|
|
|
-
|
|
|
- public static new SharedImage FromBinaryData(byte[] imageData)
|
|
|
- {
|
|
|
- return CreateImage(SKImage.FromEncodedData(imageData));
|
|
|
- }
|
|
|
-
|
|
|
- public static new SharedImage FromFile(string filePath)
|
|
|
- {
|
|
|
- return CreateImage(SKImage.FromEncodedData(filePath));
|
|
|
- }
|
|
|
-
|
|
|
- public static new SharedImage FromStream(Stream fileStream)
|
|
|
- {
|
|
|
- return CreateImage(SKImage.FromEncodedData(fileStream));
|
|
|
- }
|
|
|
-
|
|
|
- private static SharedImage CreateImage(SKImage? image)
|
|
|
- {
|
|
|
- if (image == null)
|
|
|
- throw new DocumentComposeException("Cannot load or decode provided image.");
|
|
|
-
|
|
|
- var result = new SharedImage(image);
|
|
|
-
|
|
|
- return result;
|
|
|
+ return new Image(image);
|
|
|
}
|
|
|
|
|
|
#endregion
|