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

System.Drawing.Common breaking change #25367

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/core/compatibility/6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
| [Partial and zero-byte reads in DeflateStream, GZipStream, and CryptoStream](core-libraries/6.0/partial-byte-reads-in-streams.md) | Preview 6 |
| [Some parameters in Stream-derived types are renamed](core-libraries/6.0/parameters-renamed-on-stream-derived-types.md) | Preview 1 |
| [Standard numeric format parsing precision](core-libraries/6.0/numeric-format-parsing-handles-higher-precision.md) | Preview 2 |
| [System.Drawing.Common only supported on Windows](core-libraries/6.0/system-drawing-common-windows-only.md) | Preview 7 |
| [Unhandled exceptions from a BackgroundService](core-libraries/6.0/hosting-exception-handling.md) | Preview 4 |

## Globalization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ With this change, <xref:System.IO.FileStream.ReadAsync%2A> operations are up to

- To enable the .NET 5 behavior in .NET 6, specify an `AppContext` switch or an environment variable. By setting the switch to `true`, you opt out of all performance improvements made to `FileStream` in .NET 6.

```xml
```json
{
"configProperties": {
"System.IO.UseNet5CompatFileStream": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Now, when buffering is enabled (that is, the `bufferSize` argument that's passed

- To enable the .NET 5 behavior in .NET 6, specify an `AppContext` switch or an environment variable. By setting the switch to `true`, you opt out of all performance improvements made to `FileStream` in .NET 6.

```xml
```json
{
"configProperties": {
"System.IO.UseNet5CompatFileStream": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
title: "Breaking change: System.Drawing.Common only supported on Windows"
description: Learn about the .NET 6 breaking change where the System.Drawing.Common package is no longer support on non-Windows operating systems.
ms.date: 07/27/2021
---
# System.Drawing.Common only supported on Windows

The [System.Drawing.Common](https://www.nuget.org/packages/System.Drawing.Common/) NuGet package is now attributed as a Windows-specific library. The platform analyzer emits warning at compile time when compiling for non-Windows operating systems.

On non-Windows operating systems, unless you set a run-time configuration switch, a <xref:System.TypeInitializationException> exception is thrown with <xref:System.PlatformNotSupportedException> as the inner exception.

## Old behavior

Prior to .NET 6, using the System.Drawing.Common package did not produce any compile-time warnings, and no run-time exceptions were thrown.

## New behavior

Starting in .NET 6, the platform analyzer emits compile-time warnings when referencing code is compiled for non-Windows operating systems. In addition, the following run-time exception is thrown unless you set a configuration option:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mention that in .NET 7 the plan is to no longer support this not even with the runtime config switch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs style guide says not to discuss future plans in documentation. @tdykstra @BillWagner How strictly do we adhere to this?

Copy link
Member

@safern safern Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The reason why I think in this case would be valuable, it is because we want to encourage customers to move off System.Drawing.Common for xplat and don't want this to be a surprise when we release 7.0.


```
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception.
---- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
Stack Trace:
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(42,0): at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(25,0): at System.Drawing.Bitmap..ctor(String filename)
/_/src/libraries/System.Resources.ResourceManager/tests/ResourceManagerTests.cs(270,0): at System.Resources.Tests.ResourceManagerTests.EnglishImageResourceData()+MoveNext()
/_/src/libraries/System.Linq/src/System/Linq/Select.cs(136,0): at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
----- Inner Stack Trace -----
/_/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs(31,0): at System.Drawing.LibraryResolver.EnsureRegistered()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs(65,0): at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs(27,0): at System.Drawing.SafeNativeMethods.Gdip..cctor()
```

## Version introduced

6.0 Preview 7

## Type of breaking change

This change breaks source compatibility.
gewarren marked this conversation as resolved.
Show resolved Hide resolved

## Reason for change

Because `System.Drawing.Common` was designed to be a thin wrapper over Windows technologies, its cross-platform implementation is subpar.

`libgdiplus` is the main provider of the cross-platform implementation of `System.Drawing.Common` on the native side. `libgdiplus` is effectively a reimplementation of the parts of Windows that `System.Drawing.Common` depends on. That implementation makes `libgdiplus` a non-trivial component. It's around 30,000 lines of C code that's largely untested, and it lacks a lot of functionality. `libgdiplus` also has numerous external dependencies for image processing and text rendering, such as `cairo`, `pango`, and other native libraries. Those dependencies make maintaining and shipping the component even more challenging. Since the inclusion of the Mono cross-platform implementation, we have redirected numerous issues to `libgdiplus` that never got fixed. In comparison, other external dependencies we have taken, such as `icu` or `openssl`, are high-quality libraries. It's not viable to get `libgdiplus` to the point where its feature set and quality is on par with the rest of the .NET stack.

From analysis of NuGet packages, we've observed that `System.Drawing.Common` is used cross-platform mostly for image manipulation, such as QR code generators and text rendering. We haven't noticed heavy graphics usage, as our cross-platform graphics support is incomplete. The usages we see of `System.Drawing.Common` in non-Windows environments are typically well supported with SkiaSharp and ImageSharp.

`System.Drawing.Common` will continue to evolve only in the context of Windows Forms and GDI+.

## Recommended action

To use these APIs for cross-platform apps, migrate to one of the following libraries:

- [ImageSharp](https://github.com/SixLabors/ImageSharp)
- [SkiaSharp](https://github.com/mono/SkiaSharp)
- [Microsoft.Maui.Graphics](https://github.com/dotnet/Microsoft.Maui.Graphics)

Alternatively, you can enable Unix support by setting the `System.Drawing.EnableUnixSupport` run-time configuration switch to `true` in the *runtimeconfig.json* file:
gewarren marked this conversation as resolved.
Show resolved Hide resolved
gewarren marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
}
```

This configuration switch was added to give cross-platform apps that depend heavily on this package time to migrate to more modern libraries. However, we will no longer fix non-Windows bugs.

## Affected APIs

<xref:System.Drawing?displayProperty=fullName> namespace:

- <xref:System.Drawing.Bitmap>
- <xref:System.Drawing.Brush>
- <xref:System.Drawing.Brushes>
- <xref:System.Drawing.BufferedGraphics>
- <xref:System.Drawing.BufferedGraphicsContext>
- <xref:System.Drawing.Font>
- <xref:System.Drawing.FontFamily>
- <xref:System.Drawing.FontConverter>
- <xref:System.Drawing.Graphics>
- <xref:System.Drawing.Icon>
- <xref:System.Drawing.IconConverter>
- <xref:System.Drawing.Image>
- <xref:System.Drawing.ImageAnimator>
- <xref:System.Drawing.Pen>
- <xref:System.Drawing.Pens>
- <xref:System.Drawing.Region>
- <xref:System.Drawing.SolidBrush>
- <xref:System.Drawing.StringFormat>
- <xref:System.Drawing.SystemBrushes>
- <xref:System.Drawing.SystemFonts>
- <xref:System.Drawing.SystemIcons>
- <xref:System.Drawing.SystemPens>
- <xref:System.Drawing.TextureBrush>

<xref:System.Drawing.Drawing2D?displayProperty=fullName> namespace:

- <xref:System.Drawing.Drawing2D.AdjustableArrowCap>
- <xref:System.Drawing.Drawing2D.CustomLineCap>
- <xref:System.Drawing.Drawing2D.GraphicsPath>
- <xref:System.Drawing.Drawing2D.GraphicsPathIterator>
- <xref:System.Drawing.Drawing2D.GraphicsState>
- <xref:System.Drawing.Drawing2D.HatchBrush>
- <xref:System.Drawing.Drawing2D.LinearGradientBrush>
- <xref:System.Drawing.Drawing2D.Matrix>
- <xref:System.Drawing.Drawing2D.PathGradientBrush>

<xref:System.Drawing.Imaging?displayProperty=fullName> namespace:

- <xref:System.Drawing.Imaging.Encoder>
- <xref:System.Drawing.Imaging.EncoderParameter>
- <xref:System.Drawing.Imaging.EncoderParameters>
- <xref:System.Drawing.Imaging.ImageAttributes>
- <xref:System.Drawing.Imaging.ImageCodecInfo>
- <xref:System.Drawing.Imaging.ImageFormat>
- <xref:System.Drawing.Imaging.Metafile>
- <xref:System.Drawing.Imaging.MetafileHeader>
- <xref:System.Drawing.Imaging.PlayRecordCallback>

<xref:System.Drawing.Printing?displayProperty=fullName> namespace:

- <xref:System.Drawing.Printing.PageSettings>
- <xref:System.Drawing.Printing.PreviewPageInfo>
- <xref:System.Drawing.Printing.PrintController>
- <xref:System.Drawing.Printing.PrintDocument>
- <xref:System.Drawing.Printing.PrinterSettings>
- <xref:System.Drawing.Printing.PrintEventArgs>
- <xref:System.Drawing.Printing.PrintEventHandler>
- <xref:System.Drawing.Printing.PrintPageEventArgs>
- <xref:System.Drawing.Printing.PrintPageEventHandler>

<xref:System.Drawing.Text?displayProperty=fullName> namespace:

- <xref:System.Drawing.Text.FontCollection>
- <xref:System.Drawing.Text.InstalledFontCollection>
- <xref:System.Drawing.Text.PrivateFontCollection>

## See also

- [`System.Drawing.Common` only supported on Windows - dotnet/designs](https://github.com/dotnet/designs/blob/main/accepted/2021/system-drawing-win-only/system-drawing-win-only.md)
4 changes: 4 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ items:
href: core-libraries/6.0/partial-byte-reads-in-streams.md
- name: Standard numeric format parsing precision
href: core-libraries/6.0/numeric-format-parsing-handles-higher-precision.md
- name: System.Drawing.Common only supported on Windows
href: core-libraries/6.0/system-drawing-common-windows-only.md
- name: Unhandled exceptions from a BackgroundService
href: core-libraries/6.0/hosting-exception-handling.md
- name: Globalization
Expand Down Expand Up @@ -557,6 +559,8 @@ items:
href: core-libraries/6.0/partial-byte-reads-in-streams.md
- name: Standard numeric format parsing precision
href: core-libraries/6.0/numeric-format-parsing-handles-higher-precision.md
- name: System.Drawing.Common only supported on Windows
href: core-libraries/6.0/system-drawing-common-windows-only.md
- name: Unhandled exceptions from a BackgroundService
href: core-libraries/6.0/hosting-exception-handling.md
- name: .NET 5
Expand Down