From 369ab37176f364a3fa2a1f7332c5ea8d085657e7 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Sat, 18 Apr 2020 14:29:45 -0700 Subject: [PATCH 1/4] Use GDI+1.1 API set in Windows 8 and later --- .../src/System/Drawing/GdiplusNative.cs | 5 +++- .../tests/GdiplusTests.cs | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs index c11ed0eda0e7cf..ec2262dfd036f4 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs @@ -1374,8 +1374,11 @@ internal struct StartupInput public static StartupInput GetDefault() { + OperatingSystem os = Environment.OSVersion; StartupInput result = default; - result.GdiplusVersion = 1; + + // In Windows 7 GDI+1.1 story is different as there are different binaries per GDI+ version. + result.GdiplusVersion = os.Platform != PlatformID.Unix && os.Version.Major == 6 && os.Version.Minor == 1 ? 1 : 2; // result.DebugEventCallback = null; result.SuppressBackgroundThread = false; result.SuppressExternalCodecs = false; diff --git a/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs b/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs index 69a9a84afb2b16..cf8c18b363a8ef 100644 --- a/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs +++ b/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs @@ -5,7 +5,9 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing.Text; +using System.Reflection; using Xunit; +using Xunit.Sdk; namespace System.Drawing.Tests { @@ -16,5 +18,28 @@ public void IsAtLeastLibgdiplus6() { Assert.True(Helpers.GetIsWindowsOrAtLeastLibgdiplus6()); } + + [Fact] + public void GdiplusDefaultApiVersion() + { + Type startupInput = typeof(Bitmap).Assembly.GetType("System.Drawing.SafeNativeMethods+StartupInput"); + if (startupInput != null) + { + MethodInfo methodInfo = startupInput.GetMethod("GetDefault", BindingFlags.Public | BindingFlags.Static); + if (methodInfo != null) + { + object startupInputObject = methodInfo.Invoke(null, null); + int? version = (int?)startupInput.GetField("GdiplusVersion")?.GetValue(startupInputObject); + if (version.HasValue) + { + int expectedValue = PlatformDetection.IsWindows7 ? 1 : 2; + Assert.Equal(expectedValue, version.Value); + return; + } + } + } + + throw new XunitException("Couldn't get System.Drawing.SafeNativeMethods+StartupInput.GdiplusVersion field value."); + } } } From f8f13b9d34f3b9934d4a3078fcdfe1b7f4d20a50 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Wed, 29 Apr 2020 16:27:34 -0700 Subject: [PATCH 2/4] PR Feedback, add meaningful test --- .../src/System/Drawing/GdiplusNative.cs | 2 +- .../tests/BitmapTests.cs | 30 ++++++++++++++++++- .../tests/GdiplusTests.cs | 28 ----------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs index ec2262dfd036f4..63bb136accad22 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs @@ -1378,7 +1378,7 @@ public static StartupInput GetDefault() StartupInput result = default; // In Windows 7 GDI+1.1 story is different as there are different binaries per GDI+ version. - result.GdiplusVersion = os.Platform != PlatformID.Unix && os.Version.Major == 6 && os.Version.Minor == 1 ? 1 : 2; + result.GdiplusVersion = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 1 ? 1 : 2; // result.DebugEventCallback = null; result.SuppressBackgroundThread = false; result.SuppressExternalCodecs = false; diff --git a/src/libraries/System.Drawing.Common/tests/BitmapTests.cs b/src/libraries/System.Drawing.Common/tests/BitmapTests.cs index f9123c253039c4..dfe694af913b3e 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.XUnitExtensions; using Xunit; namespace System.Drawing.Tests @@ -804,6 +805,33 @@ public void GetHicon_Disposed_ThrowsArgumentException() AssertExtensions.Throws(null, () => bitmap.GetHicon()); } + [ConditionalFact(Helpers.IsDrawingSupported)] + [PlatformSpecific(TestPlatforms.Windows)] + public void SaveWmfAsPngDoesntChangeImageBoundaries() + { + if (PlatformDetection.IsWindows7) + { + throw new SkipTestException("GDI+ 1.1 is not supported"); + } + + string output = GetTestFilePath() + ".png"; + using Stream wmfStream = File.OpenRead(Helpers.GetTestBitmapPath("gdiwmfboundariesbug.wmf")); + using Image bitmapFromWmf = Bitmap.FromStream(wmfStream); + bitmapFromWmf.Save(output, ImageFormat.Png); + + using Stream expectedPngStream = File.OpenRead(Helpers.GetTestBitmapPath("gdiwmfboundariesbug-output.png")); + using Image expectedPngBitmap = Bitmap.FromStream(expectedPngStream); + using MemoryStream expectedMemoryStream = new MemoryStream(); + expectedPngBitmap.Save(expectedMemoryStream, ImageFormat.Png); + + using Stream outputPngStream = File.OpenRead(output); + using Image outputPngBitmap = Bitmap.FromStream(outputPngStream); + using MemoryStream outputMemoryStream = new MemoryStream(); + outputPngBitmap.Save(outputMemoryStream, ImageFormat.Png); + + Assert.Equal(expectedMemoryStream.ToArray(), outputMemoryStream.ToArray()); + } + // This test causes an AV on Linux [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)] [ConditionalFact(Helpers.IsDrawingSupported)] @@ -1091,7 +1119,7 @@ public void SetResolution_Disposed_ThrowsArgumentException() public static IEnumerable LockBits_NotUnix_TestData() { Bitmap bitmap() => new Bitmap(2, 2, PixelFormat.Format32bppArgb); - yield return new object[] { bitmap(), new Rectangle(1, 1, 1,1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb, 8, 1 }; + yield return new object[] { bitmap(), new Rectangle(1, 1, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb, 8, 1 }; yield return new object[] { bitmap(), new Rectangle(1, 1, 1, 1), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb, 8, 3 }; yield return new object[] { bitmap(), new Rectangle(1, 1, 1, 1), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb, 8, 2 }; diff --git a/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs b/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs index cf8c18b363a8ef..c2719dec150a86 100644 --- a/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs +++ b/src/libraries/System.Drawing.Common/tests/GdiplusTests.cs @@ -2,12 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing.Text; -using System.Reflection; using Xunit; -using Xunit.Sdk; namespace System.Drawing.Tests { @@ -18,28 +13,5 @@ public void IsAtLeastLibgdiplus6() { Assert.True(Helpers.GetIsWindowsOrAtLeastLibgdiplus6()); } - - [Fact] - public void GdiplusDefaultApiVersion() - { - Type startupInput = typeof(Bitmap).Assembly.GetType("System.Drawing.SafeNativeMethods+StartupInput"); - if (startupInput != null) - { - MethodInfo methodInfo = startupInput.GetMethod("GetDefault", BindingFlags.Public | BindingFlags.Static); - if (methodInfo != null) - { - object startupInputObject = methodInfo.Invoke(null, null); - int? version = (int?)startupInput.GetField("GdiplusVersion")?.GetValue(startupInputObject); - if (version.HasValue) - { - int expectedValue = PlatformDetection.IsWindows7 ? 1 : 2; - Assert.Equal(expectedValue, version.Value); - return; - } - } - } - - throw new XunitException("Couldn't get System.Drawing.SafeNativeMethods+StartupInput.GdiplusVersion field value."); - } } } From eda16bc01c28854abbd4a08dd94a23d017881386 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Fri, 1 May 2020 11:23:24 -0700 Subject: [PATCH 3/4] Update System.Drawing test package from runtime-assets --- eng/Version.Details.xml | 4 ++-- eng/Versions.props | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 447d58954afaaf..167837b4c0fa83 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -90,9 +90,9 @@ https://github.com/dotnet/runtime-assets 3e5b67e9033c00da5e1ce539c0186cbf171b6ce4 - + https://github.com/dotnet/runtime-assets - 3e5b67e9033c00da5e1ce539c0186cbf171b6ce4 + 205a003b723482863e26e9d29eb7c8513ae3ded1 https://github.com/dotnet/runtime-assets diff --git a/eng/Versions.props b/eng/Versions.props index db7e0b7f5682a8..7ea8938244f20a 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -83,7 +83,7 @@ 5.0.0-alpha.1.19563.3 5.0.0-beta.20206.1 - 5.0.0-beta.20206.1 + 5.0.0-beta.20251.1 5.0.0-beta.20206.1 5.0.0-beta.20206.1 5.0.0-beta.20206.1 From 1a5f45eeb8968bc9d66187bc1a4d734f93c907fb Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Fri, 1 May 2020 11:26:29 -0700 Subject: [PATCH 4/4] PR Feedback and skip test on full framework --- .../System.Drawing.Common/src/System/Drawing/GdiplusNative.cs | 3 ++- src/libraries/System.Drawing.Common/tests/BitmapTests.cs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs index 63bb136accad22..98aadfa2be2e30 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs @@ -1378,7 +1378,8 @@ public static StartupInput GetDefault() StartupInput result = default; // In Windows 7 GDI+1.1 story is different as there are different binaries per GDI+ version. - result.GdiplusVersion = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 1 ? 1 : 2; + bool isWindows7 = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 1; + result.GdiplusVersion = isWindows7 ? 1 : 2; // result.DebugEventCallback = null; result.SuppressBackgroundThread = false; result.SuppressExternalCodecs = false; diff --git a/src/libraries/System.Drawing.Common/tests/BitmapTests.cs b/src/libraries/System.Drawing.Common/tests/BitmapTests.cs index dfe694af913b3e..e45488a74f8f83 100644 --- a/src/libraries/System.Drawing.Common/tests/BitmapTests.cs +++ b/src/libraries/System.Drawing.Common/tests/BitmapTests.cs @@ -807,6 +807,7 @@ public void GetHicon_Disposed_ThrowsArgumentException() [ConditionalFact(Helpers.IsDrawingSupported)] [PlatformSpecific(TestPlatforms.Windows)] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "In .NET Framework we use GDI 1.0")] public void SaveWmfAsPngDoesntChangeImageBoundaries() { if (PlatformDetection.IsWindows7)