Cannot see the Image type in System.Drawing namespace in .NET
Asked Answered
C

3

19

I'm trying to write a program that sorts images in specific folder by ther dimensions and moves little images to another folder via simple .NET console application. I decided to use System.Drawing.Image class to get the image dimentions from an image file. But I face following error:

The type or namespace name 'Image' could not be found (are you missing a using directive or an assembly referrence?)

What exactly did I do wrong and why it doesn't see this class? Here are the complete code of my program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;

namespace ImageSort
{
    class Program
    {
        static void Main(string[] args)
        {
            string targetPath = @"d:\SmallImages";
            string[] files = Directory.GetFiles(@"d:\Images");
            foreach (string path in files)
            {
                if (File.Exists(path))
                {
                    Image newImage = Image.FromFile(path);
                    var Width = (int)(newImage.Width);
                    var Height = (int)(newImage.Height);
                    if (Width * Height < 660000) {
                        System.IO.File.Move(path, targetPath);
                    }
                }
            }
        }
    }
}
Crystlecs answered 6/5, 2016 at 22:34 Comment(0)
G
19

You need to add a reference : System.Drawing.dll.

In Solution Explorer, right-click on the References node and choose Add Reference and find System.Drawing.dll.

Gipson answered 6/5, 2016 at 22:41 Comment(5)
Yes, it helped! I was such a rookie in this. Thanks!Crystlecs
This is such a huge help for a rookie.Basidiomycete
Why? Why do we have to do this for System.Drawing but nothing else that I've seen so far?Archivist
@Clonkex, you might find some answers on this Microsoft System.Drawing.Common only supported on Windows article.Crock
Which one to use? 2.0 of 2.4?Simonetta
C
17

The answer to this issue for .NET Core 3.1 is to simply install System.Drawing.Common from NuGet.

Crock answered 17/2, 2022 at 15:32 Comment(0)
G
1

Just to add to @feganmeister answer, you can install the NuGet package using dotnet CLI command from a terminal:

Navigate to the project directory and run the following command:

cd <project-directory>
dotnet add package System.Drawing.Common
Grondin answered 14/2, 2024 at 15:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.