Search code examples
c#visual-studio

'Bitmap' could not be found in the namespace 'System.Drawing'


I'm having trouble getting the Bitmap class to work properly in my C# project. Despite extensive searching, the common advice I find is to "Add reference to System.Drawing", which I have tried to do (I think).

Here's what I've done so far:

  • I went to Project -> Add COM Reference -> Browse and then selected System.Drawing.VisualStudio.dll.

However, the solutions I found online suggest using Add reference, but in my Visual Studio, I only see options for Add Project Reference, Add COM Reference, and Add Shared Project Reference.

Here is the code I'm working with:

using System;
using System.Drawing;

static void Main(string[] args)
{
    Bitmap myBit = new Bitmap("url"); // ERROR
}

I'm getting the following error:

CS1069: The type name 'Bitmap' could not be found in the namespace 'System.Drawing'

Visual Studio Version: Visual Studio 2019 version 16.11.36

How can I correctly reference the System.Drawing namespace and use the Bitmap class?


Solution

  • I've added the reference via Project -> Add COM reference -> Browse and then select "Sytem.drawing.visualstudio.dll"

    You shouldn't be adding a COM reference and that's not the correct library to add. See below to add the correct reference to use the System.Drawing namespace that has Bitmap class.

    1. Right click on References under your project.
    2. Select Add Reference.
    3. Click on Assemblies.
    4. In the top right -> Search -> system.draw etc...
    5. A few items should show, select System.Drawing (mine was 4.0.0.0) make sure it's checked.
    6. Click OK.

    You should now see under References under your project the namespace.

    enter image description here