Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw DirectoryNotFoundException on Image.Save with bad directory #34998

Merged
merged 9 commits into from
Apr 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,7 @@
<data name="ValueNotOneOfValues" xml:space="preserve">
<value>The value of the {0} property is not one of the {1} values</value>
</data>
<data name="TargetDirectoryDoesNotExist" xml:space="preserve">
<value>The directory {0} of the filename {1} does not exist.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public void Save(string filename, ImageFormat format)

public void Save(string filename, ImageCodecInfo encoder, EncoderParameters? encoderParams)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));

ThrowIfDirectoryDoesntExist(filename);

int st;
Guid guid = encoder.Clsid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public void Save(string filename, ImageCodecInfo encoder, EncoderParameters? enc
if (encoder == null)
throw new ArgumentNullException(nameof(encoder));

ThrowIfDirectoryDoesntExist(filename);

IntPtr encoderParamsMemory = IntPtr.Zero;

if (encoderParams != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ public void Dispose()
/// </summary>
public void Save(string filename) => Save(filename, RawFormat);

private static void ThrowIfDirectoryDoesntExist(string filename)
{
var directoryPart = System.IO.Path.GetDirectoryName(filename);
if (!string.IsNullOrEmpty(directoryPart) && !System.IO.Directory.Exists(directoryPart))
{
throw new DirectoryNotFoundException(SR.Format(SR.TargetDirectoryDoesNotExist, directoryPart, filename));
}
}

/// <summary>
/// Gets the width and height of this <see cref='Image'/>.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/libraries/System.Drawing.Common/tests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,16 @@ public void GetEncoderParameterList_ReturnsExpected(ImageFormat format, Guid[] e
paramList.Param.Select(p => p.Encoder.Guid));
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework throws ExternalException")]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void Save_InvalidDirecotry_ThrowsDirectoryNotFoundException()
safern marked this conversation as resolved.
Show resolved Hide resolved
{
using (var bitmap = new Bitmap(1, 1))
{
var badTarget = System.IO.Path.Combine("NoSuchDirectory", "NoSuchFile");
AssertExtensions.Throws<DirectoryNotFoundException>(() => bitmap.Save(badTarget), $"The directory NoSuchDirectory of the filename {badTarget} does not exist.");
}
}
}
}