VS2017中F#/.NET Standard2.0项目无法识别System.Drawing.Bitmap
Hey there, let's break down why you're running into this issue and how to fix it quickly. The core problem here is that .NET Standard 2.0 doesn't include the full System.Drawing APIs out of the box—the traditional System.Drawing reference you're trying to add is tied to .NET Framework, not .NET Standard. Here's what you need to do:
Step 1: Remove any existing System.Drawing references
First, get rid of any manual System.Drawing references you added to your project. These are likely pointing to the .NET Framework version, which won't work properly with .NET Standard.
- Right-click your project in Solution Explorer > Manage NuGet Packages
- Go to the "Installed" tab, find any
System.Drawingentries, and uninstall them - Also check your project file (
.fsproj) to make sure there are no leftover<Reference Include="System.Drawing" />lines—delete them if present
Step 2: Install the official System.Drawing.Common NuGet package
Microsoft provides a dedicated NuGet package for System.Drawing in .NET Standard/.NET Core projects: System.Drawing.Common. This is the only supported way to use Bitmap and other GDI+ APIs in your project.
You can install it in two ways:
Option A: Using Visual Studio's NuGet Package Manager
- Right-click your project > Manage NuGet Packages
- Go to the "Browse" tab, search for
System.Drawing.Common - Select the package (make sure it's published by Microsoft) and click "Install"
Option B: Using the .NET CLI
Open a terminal in your project directory and run:
dotnet add package System.Drawing.Common
Step 3: Verify the reference is correctly added
Check your .fsproj file to confirm the package is listed. You should see a line like this:
<PackageReference Include="System.Drawing.Common" Version="x.x.x" />
(Replace x.x.x with the stable version you installed—ensure it's compatible with .NET Standard 2.0.)
Step 4: Test with a simple F# code snippet
Add a quick test to make sure Bitmap is now recognized:
open System.Drawing let testBitmap = new Bitmap(100, 100) // Do something with testBitmap, like save it testBitmap.Save("test.png") testBitmap.Dispose()
Build your project—this should compile without the "type not defined" error now.
Important Notes
- Cross-platform considerations: While
System.Drawing.Commonworks seamlessly on Windows, it has limited support on Linux/macOS (it requires libgdiplus to be installed). If you're targeting non-Windows platforms, consider alternatives likeImageSharporSkiaSharpfor image processing. - Avoid mixing references: Never add both the .NET Framework
System.Drawingreference and theSystem.Drawing.CommonNuGet package—this will cause conflicts.
内容的提问来源于stack exchange,提问作者Aphex

