Skip to content

Commit f9ea016

Browse files
authored
Merge pull request #12504 from JeremyKuhne/port12501
Ensure GDI+ is initialized for all codepaths
2 parents 25339a0 + 89b697b commit f9ea016

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static ImageCodecInfo[] GetImageDecoders()
5757

5858
public static ImageCodecInfo[] GetImageEncoders()
5959
{
60+
GdiPlusInitialization.EnsureInitialized();
61+
6062
ImageCodecInfo[] imageCodecs;
6163
uint numEncoders;
6264
uint size;

src/System.Private.Windows.Core/src/Windows/Win32/Graphics/GdiPlus/GdiPlusInitialization.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Windows.Win32.Graphics.GdiPlus;
55

6+
/// <summary>
7+
/// Helper to ensure GDI+ is initialized before making calls.
8+
/// </summary>
69
internal static partial class GdiPlusInitialization
710
{
811
private static readonly nuint s_initToken = Init();
@@ -23,5 +26,18 @@ private static unsafe nuint Init()
2326
/// <summary>
2427
/// Returns true if GDI+ has been started.
2528
/// </summary>
29+
/// <remarks>
30+
/// <para>
31+
/// This should be called anywhere you make <see cref="PInvokeCore"/> calls to GDI+ where you don't
32+
/// already have a GDI+ handle. In System.Drawing.Common, this is done in the PInvoke static constructor
33+
/// so it is not necessary for methods defined there.
34+
/// </para>
35+
/// <para>
36+
/// We don't do this implicitly in the Core assembly to avoid unnecessary loading of GDI+.
37+
/// </para>
38+
/// <para>
39+
/// https://github.com/microsoft/CsWin32/issues/1308 tracks a proposal to make this more automatic.
40+
/// </para>
41+
/// </remarks>
2642
internal static bool EnsureInitialized() => s_initToken != 0;
2743
}

0 commit comments

Comments
 (0)