Skip to content

Commit 098c42f

Browse files
Expose FormattedImage constructor
1 parent 7980881 commit 098c42f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/ImageSharp.Web/FormattedImage.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace SixLabors.ImageSharp.Web
1919
public sealed class FormattedImage : IDisposable
2020
{
2121
private readonly ImageFormatManager imageFormatsManager;
22+
private readonly bool keepOpen;
2223
private IImageFormat format;
2324
private IImageEncoder encoder;
2425

@@ -27,11 +28,23 @@ public sealed class FormattedImage : IDisposable
2728
/// </summary>
2829
/// <param name="image">The image.</param>
2930
/// <param name="format">The format.</param>
30-
internal FormattedImage(Image image, IImageFormat format)
31+
public FormattedImage(Image image, IImageFormat format)
32+
: this(image, format, true)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="FormattedImage"/> class.
38+
/// </summary>
39+
/// <param name="image">The image.</param>
40+
/// <param name="format">The format.</param>
41+
/// <param name="keepOpen">Whether to keep the source image open upon disposing the <see cref="FormattedImage"/> object.</param>
42+
private FormattedImage(Image image, IImageFormat format, bool keepOpen)
3143
{
3244
this.Image = image;
3345
this.imageFormatsManager = image.GetConfiguration().ImageFormatsManager;
3446
this.Format = format;
47+
this.keepOpen = keepOpen;
3548
}
3649

3750
/// <summary>
@@ -92,7 +105,7 @@ internal static async Task<FormattedImage> LoadAsync<TPixel>(Configuration confi
92105
where TPixel : unmanaged, IPixel<TPixel>
93106
{
94107
(Image<TPixel> image, IImageFormat format) = await Image.LoadWithFormatAsync<TPixel>(configuration, source);
95-
return new FormattedImage(image, format);
108+
return new FormattedImage(image, format, false);
96109
}
97110

98111
/// <summary>
@@ -104,7 +117,7 @@ internal static async Task<FormattedImage> LoadAsync<TPixel>(Configuration confi
104117
internal static async Task<FormattedImage> LoadAsync(Configuration configuration, Stream source)
105118
{
106119
(Image image, IImageFormat format) = await Image.LoadWithFormatAsync(configuration, source);
107-
return new FormattedImage(image, format);
120+
return new FormattedImage(image, format, false);
108121
}
109122

110123
/// <summary>
@@ -157,8 +170,11 @@ public bool TryGetExifOrientation(out ushort value)
157170
/// </summary>
158171
public void Dispose()
159172
{
160-
this.Image?.Dispose();
161-
this.Image = null;
173+
if (!this.keepOpen)
174+
{
175+
this.Image?.Dispose();
176+
this.Image = null;
177+
}
162178
}
163179

164180
[MethodImpl(MethodImplOptions.NoInlining)]

0 commit comments

Comments
 (0)