diff --git a/src/libraries/System.Drawing.Common/Directory.Build.props b/src/libraries/System.Drawing.Common/Directory.Build.props index addd07be3bbd84..9ef62c61e92ff3 100644 --- a/src/libraries/System.Drawing.Common/Directory.Build.props +++ b/src/libraries/System.Drawing.Common/Directory.Build.props @@ -2,8 +2,7 @@ Open - true - browser + windows Provides access to GDI+ graphics functionality. Commonly Used Types: @@ -12,6 +11,8 @@ System.Drawing.BitmapData System.Drawing.Brush System.Drawing.Font System.Drawing.Graphics -System.Drawing.Icon +System.Drawing.Icon + +Unix support is disabled by default. See https://aka.ms/systemdrawingnonwindows for more information. \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Resources/Strings.resx b/src/libraries/System.Drawing.Common/src/Resources/Strings.resx index 969bd00ec5e7ab..2d3d8134aabce0 100644 --- a/src/libraries/System.Drawing.Common/src/Resources/Strings.resx +++ b/src/libraries/System.Drawing.Common/src/Resources/Strings.resx @@ -353,6 +353,9 @@ System.Drawing is not supported on this platform. + + System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information. + Defines an object that sends output to a printer. @@ -464,4 +467,4 @@ System.Drawing.Common is not supported on this platform. - \ No newline at end of file + diff --git a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj index ff707877c2d11c..f571be1d8a58f2 100644 --- a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj +++ b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj @@ -170,6 +170,8 @@ Link="Common\Interop\Windows\User32\Interop.LOGFONT.cs" /> + - + @@ -243,8 +245,6 @@ - + diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs index 8f10899f850e7f..cb4bdf3c73b35c 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs @@ -27,7 +27,8 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D internal static void EnsureRegistered() { - // dummy call to trigger the static constructor + if (!LocalAppContextSwitches.EnableUnixSupport) + throw new PlatformNotSupportedException(SR.PlatformNotSupported_Unix); } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs new file mode 100644 index 00000000000000..44fe88b2f6ea91 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; + +namespace System +{ + internal static partial class LocalAppContextSwitches + { + private static int s_enableUnixSupport; + public static bool EnableUnixSupport + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return GetCachedSwitchValue(@"System.Drawing.EnableUnixSupport", ref s_enableUnixSupport); + } + } + } +} diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Windows.cs similarity index 100% rename from src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.cs rename to src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Windows.cs diff --git a/src/libraries/System.Drawing.Common/tests/BitmapTests.cs b/src/libraries/System.Drawing.Common/tests/BitmapTests.cs index d861dbe08153b3..0b15dfc7821caa 100644 --- a/src/libraries/System.Drawing.Common/tests/BitmapTests.cs +++ b/src/libraries/System.Drawing.Common/tests/BitmapTests.cs @@ -29,6 +29,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using Microsoft.DotNet.RemoteExecutor; using Microsoft.DotNet.XUnitExtensions; using Xunit; @@ -44,6 +45,18 @@ public static IEnumerable Ctor_FilePath_TestData() yield return new object[] { "16x16_nonindexed_24bit.png", 16, 16, PixelFormat.Format24bppRgb, ImageFormat.Png }; } + [PlatformSpecific(TestPlatforms.AnyUnix)] + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void UnixSupportDisabledThrows() + { + RemoteExecutor.Invoke(() => + { + AppContext.SetSwitch("System.Drawing.EnableUnixSupport", false); + TypeInitializationException exception = Assert.Throws(() => new Bitmap(100, 100)); + Assert.IsType(exception.InnerException); + }).Dispose(); + } + [ConditionalTheory(Helpers.IsDrawingSupported)] [MemberData(nameof(Ctor_FilePath_TestData))] public void Ctor_FilePath(string filename, int width, int height, PixelFormat pixelFormat, ImageFormat rawFormat) diff --git a/src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json b/src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json new file mode 100644 index 00000000000000..e3ad204dd9e512 --- /dev/null +++ b/src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json @@ -0,0 +1,5 @@ +{ + "configProperties": { + "System.Drawing.EnableUnixSupport": true + } +} \ No newline at end of file diff --git a/src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json b/src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json new file mode 100644 index 00000000000000..e3ad204dd9e512 --- /dev/null +++ b/src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json @@ -0,0 +1,5 @@ +{ + "configProperties": { + "System.Drawing.EnableUnixSupport": true + } +} \ No newline at end of file diff --git a/src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json b/src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json new file mode 100644 index 00000000000000..e3ad204dd9e512 --- /dev/null +++ b/src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json @@ -0,0 +1,5 @@ +{ + "configProperties": { + "System.Drawing.EnableUnixSupport": true + } +} \ No newline at end of file diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json b/src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json new file mode 100644 index 00000000000000..e3ad204dd9e512 --- /dev/null +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json @@ -0,0 +1,5 @@ +{ + "configProperties": { + "System.Drawing.EnableUnixSupport": true + } +} \ No newline at end of file