Skip to content

Commit 893fee5

Browse files
committed
Use file name implementation instead of file one in all the read/ping/write methods.
1 parent 5073ccc commit 893fee5

6 files changed

Lines changed: 201 additions & 80 deletions

File tree

‎src/Magick.NET/Formats/Pdf/PdfInfo.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ private PdfInfo(uint pageCount)
2626
/// <param name="file">The pdf file to create the info from.</param>
2727
/// <returns>The info of a <see cref="MagickFormat.Pdf"/> file.</returns>
2828
public static PdfInfo Create(FileInfo file)
29-
=> Create(file, string.Empty);
29+
{
30+
Throw.IfNull(file);
31+
32+
return Create(file.FullName);
33+
}
3034

3135
/// <summary>
3236
/// Creates info from a <see cref="MagickFormat.Pdf"/> file.
@@ -37,6 +41,7 @@ public static PdfInfo Create(FileInfo file)
3741
public static PdfInfo Create(FileInfo file, string password)
3842
{
3943
Throw.IfNull(file);
44+
4045
return Create(file.FullName, password);
4146
}
4247

‎src/Magick.NET/MagickFormatInfo.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ internal static IReadOnlyCollection<IMagickFormatInfo> All
8686
{
8787
Throw.IfNull(file);
8888

89-
var format = EnumHelper.ParseMagickFormatFromExtension(file);
90-
91-
if (format == MagickFormat.Unknown)
92-
return null;
93-
94-
return Create(format);
89+
return Create(file.FullName);
9590
}
9691

9792
/// <summary>
@@ -130,7 +125,12 @@ internal static IReadOnlyCollection<IMagickFormatInfo> All
130125
{
131126
var filePath = FileHelper.CheckForBaseDirectory(fileName);
132127

133-
return Create(new FileInfo(filePath));
128+
var format = EnumHelper.ParseMagickFormatFromExtension(filePath);
129+
130+
if (format == MagickFormat.Unknown)
131+
return null;
132+
133+
return Create(format);
134134
}
135135

136136
/// <summary>

‎src/Magick.NET/MagickImage.cs‎

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,7 +4427,12 @@ public void Ping(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
44274427
/// </summary>
44284428
/// <param name="file">The file to read the image from.</param>
44294429
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
4430-
public void Ping(FileInfo file) => Ping(file, null);
4430+
public void Ping(FileInfo file)
4431+
{
4432+
Throw.IfNull(file);
4433+
4434+
Ping(file.FullName);
4435+
}
44314436

44324437
/// <summary>
44334438
/// Reads only metadata and not the pixel data.
@@ -4439,7 +4444,7 @@ public void Ping(FileInfo file, IMagickReadSettings<QuantumType>? readSettings)
44394444
{
44404445
Throw.IfNull(file);
44414446

4442-
Read(file.FullName, readSettings, true);
4447+
Ping(file.FullName, readSettings);
44434448
}
44444449

44454450
/// <summary>
@@ -4706,7 +4711,11 @@ public void Read(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
47064711
/// <param name="file">The file to read the image from.</param>
47074712
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
47084713
public void Read(FileInfo file)
4709-
=> Read(file, null);
4714+
{
4715+
Throw.IfNull(file);
4716+
4717+
Read(file.FullName);
4718+
}
47104719

47114720
/// <summary>
47124721
/// Read single image frame.
@@ -4732,7 +4741,7 @@ public void Read(FileInfo file, MagickFormat format)
47324741
{
47334742
Throw.IfNull(file);
47344743

4735-
Read(file.FullName, new MagickReadSettings(_settings) { Format = format });
4744+
Read(file.FullName, format);
47364745
}
47374746

47384747
/// <summary>
@@ -4843,7 +4852,11 @@ public void Read(string fileName, IMagickReadSettings<QuantumType>? readSettings
48434852
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
48444853
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
48454854
public Task ReadAsync(FileInfo file)
4846-
=> ReadAsync(file, CancellationToken.None);
4855+
{
4856+
Throw.IfNull(file);
4857+
4858+
return ReadAsync(file.FullName);
4859+
}
48474860

48484861
/// <summary>
48494862
/// Read single image frame.
@@ -4853,7 +4866,11 @@ public Task ReadAsync(FileInfo file)
48534866
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
48544867
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
48554868
public Task ReadAsync(FileInfo file, CancellationToken cancellationToken)
4856-
=> ReadAsync(file, null, cancellationToken);
4869+
{
4870+
Throw.IfNull(file);
4871+
4872+
return ReadAsync(file.FullName, cancellationToken);
4873+
}
48574874

48584875
/// <summary>
48594876
/// Read single image frame.
@@ -4863,7 +4880,11 @@ public Task ReadAsync(FileInfo file, CancellationToken cancellationToken)
48634880
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
48644881
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
48654882
public Task ReadAsync(FileInfo file, MagickFormat format)
4866-
=> ReadAsync(file, format, CancellationToken.None);
4883+
{
4884+
Throw.IfNull(file);
4885+
4886+
return ReadAsync(file.FullName, format);
4887+
}
48674888

48684889
/// <summary>
48694890
/// Read single image frame.
@@ -4874,7 +4895,11 @@ public Task ReadAsync(FileInfo file, MagickFormat format)
48744895
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
48754896
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
48764897
public Task ReadAsync(FileInfo file, MagickFormat format, CancellationToken cancellationToken)
4877-
=> ReadAsync(file, new MagickReadSettings(_settings) { Format = format }, cancellationToken);
4898+
{
4899+
Throw.IfNull(file);
4900+
4901+
return ReadAsync(file.FullName, format, cancellationToken);
4902+
}
48784903

48794904
/// <summary>
48804905
/// Read single image frame.
@@ -4884,7 +4909,11 @@ public Task ReadAsync(FileInfo file, MagickFormat format, CancellationToken canc
48844909
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
48854910
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
48864911
public Task ReadAsync(FileInfo file, IMagickReadSettings<QuantumType>? readSettings)
4887-
=> ReadAsync(file, readSettings, CancellationToken.None);
4912+
{
4913+
Throw.IfNull(file);
4914+
4915+
return ReadAsync(file.FullName, readSettings);
4916+
}
48884917

48894918
/// <summary>
48904919
/// Read single image frame.
@@ -5168,7 +5197,11 @@ public void ReadPixels(string fileName, IPixelReadSettings<QuantumType> settings
51685197
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
51695198
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
51705199
public Task ReadPixelsAsync(FileInfo file, IPixelReadSettings<QuantumType> settings)
5171-
=> ReadPixelsAsync(file, settings, CancellationToken.None);
5200+
{
5201+
Throw.IfNull(file);
5202+
5203+
return ReadPixelsAsync(file.FullName, settings);
5204+
}
51725205

51735206
/// <summary>
51745207
/// Read single image frame from pixel data.
@@ -7141,8 +7174,10 @@ public void Write(FileInfo file)
71417174
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
71427175
public void Write(FileInfo file, IWriteDefines defines)
71437176
{
7144-
_settings.SetDefines(defines);
7145-
Write(file, defines.Format);
7177+
Throw.IfNull(file);
7178+
7179+
Write(file.FullName, defines);
7180+
file.Refresh();
71467181
}
71477182

71487183
/// <summary>
@@ -7153,8 +7188,10 @@ public void Write(FileInfo file, IWriteDefines defines)
71537188
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
71547189
public void Write(FileInfo file, MagickFormat format)
71557190
{
7156-
using var tempFormat = new TemporaryMagickFormat(this, format);
7157-
Write(file);
7191+
Throw.IfNull(file);
7192+
7193+
Write(file.FullName, format);
7194+
file.Refresh();
71587195
}
71597196

71607197
/// <summary>
@@ -7253,8 +7290,13 @@ public void Write(string fileName, MagickFormat format)
72537290
/// <param name="file">The file to write the image to.</param>
72547291
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
72557292
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7256-
public Task WriteAsync(FileInfo file)
7257-
=> WriteAsync(file, CancellationToken.None);
7293+
public async Task WriteAsync(FileInfo file)
7294+
{
7295+
Throw.IfNull(file);
7296+
7297+
await WriteAsync(file.FullName);
7298+
file.Refresh();
7299+
}
72587300

72597301
/// <summary>
72607302
/// Writes the image to the specified file.
@@ -7263,13 +7305,12 @@ public Task WriteAsync(FileInfo file)
72637305
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
72647306
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
72657307
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7266-
public Task WriteAsync(FileInfo file, CancellationToken cancellationToken)
7308+
public async Task WriteAsync(FileInfo file, CancellationToken cancellationToken)
72677309
{
72687310
Throw.IfNull(file);
72697311

7270-
var format = EnumHelper.ParseMagickFormatFromExtension(file);
7271-
var bytes = format != MagickFormat.Unknown ? ToByteArray(format) : ToByteArray();
7272-
return FileHelper.WriteAllBytesAsync(file.FullName, bytes, cancellationToken);
7312+
await WriteAsync(file.FullName, cancellationToken);
7313+
file.Refresh();
72737314
}
72747315

72757316
/// <summary>
@@ -7279,8 +7320,13 @@ public Task WriteAsync(FileInfo file, CancellationToken cancellationToken)
72797320
/// <param name="defines">The defines to set.</param>
72807321
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
72817322
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7282-
public Task WriteAsync(FileInfo file, IWriteDefines defines)
7283-
=> WriteAsync(file, defines, CancellationToken.None);
7323+
public async Task WriteAsync(FileInfo file, IWriteDefines defines)
7324+
{
7325+
Throw.IfNull(file);
7326+
7327+
await WriteAsync(file, defines);
7328+
file.Refresh();
7329+
}
72847330

72857331
/// <summary>
72867332
/// Writes the image to the specified file.
@@ -7290,10 +7336,12 @@ public Task WriteAsync(FileInfo file, IWriteDefines defines)
72907336
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
72917337
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
72927338
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7293-
public Task WriteAsync(FileInfo file, IWriteDefines defines, CancellationToken cancellationToken)
7339+
public async Task WriteAsync(FileInfo file, IWriteDefines defines, CancellationToken cancellationToken)
72947340
{
7295-
_settings.SetDefines(defines);
7296-
return WriteAsync(file, defines.Format, cancellationToken);
7341+
Throw.IfNull(file);
7342+
7343+
await WriteAsync(file.FullName, defines, cancellationToken);
7344+
file.Refresh();
72977345
}
72987346

72997347
/// <summary>
@@ -7303,8 +7351,13 @@ public Task WriteAsync(FileInfo file, IWriteDefines defines, CancellationToken c
73037351
/// <param name="format">The format to use.</param>
73047352
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
73057353
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7306-
public Task WriteAsync(FileInfo file, MagickFormat format)
7307-
=> WriteAsync(file, format, CancellationToken.None);
7354+
public async Task WriteAsync(FileInfo file, MagickFormat format)
7355+
{
7356+
Throw.IfNull(file);
7357+
7358+
await WriteAsync(file, format);
7359+
file.Refresh();
7360+
}
73087361

73097362
/// <summary>
73107363
/// Writes the image to the specified file.
@@ -7314,12 +7367,12 @@ public Task WriteAsync(FileInfo file, MagickFormat format)
73147367
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
73157368
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
73167369
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
7317-
public Task WriteAsync(FileInfo file, MagickFormat format, CancellationToken cancellationToken)
7370+
public async Task WriteAsync(FileInfo file, MagickFormat format, CancellationToken cancellationToken)
73187371
{
73197372
Throw.IfNull(file);
73207373

7321-
var bytes = ToByteArray(format);
7322-
return FileHelper.WriteAllBytesAsync(file.FullName, bytes, cancellationToken);
7374+
await WriteAsync(file.FullName, format, cancellationToken);
7375+
file.Refresh();
73237376
}
73247377

73257378
/// <summary>
@@ -7414,7 +7467,9 @@ public Task WriteAsync(string fileName, CancellationToken cancellationToken)
74147467
{
74157468
var filePath = FileHelper.CheckForBaseDirectory(fileName);
74167469

7417-
return WriteAsync(new FileInfo(filePath), cancellationToken);
7470+
var format = EnumHelper.ParseMagickFormatFromExtension(filePath);
7471+
var bytes = format != MagickFormat.Unknown ? ToByteArray(format) : ToByteArray();
7472+
return FileHelper.WriteAllBytesAsync(filePath, bytes, cancellationToken);
74187473
}
74197474

74207475
/// <summary>

0 commit comments

Comments
 (0)