Skip to content

Commit d2a096c

Browse files
committed
Added WriteAsync to MagickImage.
1 parent 11a1fa5 commit d2a096c

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

src/Magick.NET.Core/IMagickImage.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,5 +3646,33 @@ public interface IMagickImage : IDisposable
36463646
/// <param name="format">The format to use.</param>
36473647
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
36483648
void Write(string fileName, MagickFormat format);
3649+
3650+
#if NETSTANDARD
3651+
/// <summary>
3652+
/// Writes the image to the specified stream.
3653+
/// </summary>
3654+
/// <param name="stream">The stream to write the image data to.</param>
3655+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
3656+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
3657+
Task WriteAsync(Stream stream);
3658+
3659+
/// <summary>
3660+
/// Writes the image to the specified stream.
3661+
/// </summary>
3662+
/// <param name="stream">The stream to write the image data to.</param>
3663+
/// <param name="defines">The defines to set.</param>
3664+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
3665+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
3666+
Task WriteAsync(Stream stream, IWriteDefines defines);
3667+
3668+
/// <summary>
3669+
/// Writes the image to the specified stream.
3670+
/// </summary>
3671+
/// <param name="stream">The stream to write the image data to.</param>
3672+
/// <param name="format">The format to use.</param>
3673+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
3674+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
3675+
Task WriteAsync(Stream stream, MagickFormat format);
3676+
#endif
36493677
}
36503678
}

src/Magick.NET/MagickImage.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6703,7 +6703,7 @@ public void Write(Stream stream)
67036703
if (stream.CanRead)
67046704
reader = new ReadWriteStreamDelegate(wrapper.Read);
67056705

6706-
_nativeInstance.WriteStream(Settings, writer, seeker, teller, reader);
6706+
_nativeInstance.WriteStream(_settings, writer, seeker, teller, reader);
67076707
}
67086708
}
67096709

@@ -6746,7 +6746,7 @@ public void Write(string fileName)
67466746
Throw.IfNullOrEmpty(nameof(fileName), filePath);
67476747

67486748
_nativeInstance.FileName = filePath;
6749-
_nativeInstance.WriteFile(Settings);
6749+
_nativeInstance.WriteFile(_settings);
67506750
}
67516751

67526752
/// <summary>
@@ -6773,6 +6773,51 @@ public void Write(string fileName, MagickFormat format)
67736773
Write(fileName);
67746774
}
67756775

6776+
#if NETSTANDARD
6777+
/// <summary>
6778+
/// Writes the image to the specified stream.
6779+
/// </summary>
6780+
/// <param name="stream">The stream to write the image data to.</param>
6781+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
6782+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
6783+
public Task WriteAsync(Stream stream)
6784+
{
6785+
Throw.IfNull(nameof(stream), stream);
6786+
6787+
var bytes = ToByteArray();
6788+
return stream.WriteAsync(bytes, 0, bytes.Length);
6789+
}
6790+
6791+
/// <summary>
6792+
/// Writes the image to the specified stream.
6793+
/// </summary>
6794+
/// <param name="stream">The stream to write the image data to.</param>
6795+
/// <param name="defines">The defines to set.</param>
6796+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
6797+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
6798+
public Task WriteAsync(Stream stream, IWriteDefines defines)
6799+
{
6800+
_settings.SetDefines(defines);
6801+
_settings.Format = defines.Format;
6802+
return WriteAsync(stream);
6803+
}
6804+
6805+
/// <summary>
6806+
/// Writes the image to the specified stream.
6807+
/// </summary>
6808+
/// <param name="stream">The stream to write the image data to.</param>
6809+
/// <param name="format">The format to use.</param>
6810+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
6811+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
6812+
public async Task WriteAsync(Stream stream, MagickFormat format)
6813+
{
6814+
var currentFormat = Format;
6815+
_settings.Format = format;
6816+
await WriteAsync(stream).ConfigureAwait(false);
6817+
Format = currentFormat;
6818+
}
6819+
#endif
6820+
67766821
internal static IMagickImage<QuantumType> Clone(IMagickImage<QuantumType> image)
67776822
=> image?.Clone();
67786823

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2013-2020 Dirk Lemstra <https://github.com/dlemstra/Magick.NET/>
2+
//
3+
// Licensed under the ImageMagick License (the "License"); you may not use this file except in
4+
// compliance with the License. You may obtain a copy of the License at
5+
//
6+
// https://www.imagemagick.org/script/license.php
7+
//
8+
// Unless required by applicable law or agreed to in writing, software distributed under the
9+
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10+
// either express or implied. See the License for the specific language governing permissions
11+
// and limitations under the License.
12+
13+
#if NETCOREAPP
14+
using System;
15+
using System.IO;
16+
using System.Threading.Tasks;
17+
using ImageMagick;
18+
using Xunit;
19+
20+
namespace Magick.NET.Tests
21+
{
22+
public partial class MagickImageTests
23+
{
24+
public class TheWriteAsyncMethod
25+
{
26+
public class WithStream
27+
{
28+
[Fact]
29+
public async Task ShouldThrowExceptionWhenStreamIsNull()
30+
{
31+
using (var image = new MagickImage())
32+
{
33+
await Assert.ThrowsAsync<ArgumentNullException>("stream", () => image.WriteAsync(null));
34+
}
35+
}
36+
}
37+
38+
public class WithStreamAndMagickFormat
39+
{
40+
[Fact]
41+
public async Task ShouldThrowExceptionWhenStreamIsNull()
42+
{
43+
using (var image = new MagickImage())
44+
{
45+
await Assert.ThrowsAsync<ArgumentNullException>("stream", () => image.WriteAsync(null, MagickFormat.Bmp));
46+
}
47+
}
48+
49+
[Fact]
50+
public async Task ShouldUseTheSpecifiedFormat()
51+
{
52+
using (var input = new MagickImage(Files.CirclePNG))
53+
{
54+
using (var memoryStream = new MemoryStream())
55+
{
56+
using (var stream = new NonSeekableStream(memoryStream))
57+
{
58+
await input.WriteAsync(stream, MagickFormat.Tiff);
59+
Assert.Equal(MagickFormat.Png, input.Format);
60+
61+
memoryStream.Position = 0;
62+
using (var output = new MagickImage(stream))
63+
{
64+
Assert.Equal(MagickFormat.Tiff, output.Format);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
#endif

0 commit comments

Comments
 (0)