我正在尝试编写一个程序,按尺寸对特定文件夹中的图像进行排序,并通过简单的 .NET 控制台应用程序将小图像移动到另一个文件夹。我决定使用 System.Drawing.Image 类从图像文件中获取图像尺寸。但我面临以下错误:
找不到类型或命名空间名称“Image”(您是否缺少 using 指令还是程序集引用?)
我到底做错了什么以及为什么它看不到这个课程? 这是我的程序的完整代码:
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);
}
}
}
}
}
}
您需要添加参考:
System.Drawing.dll.
在
Solution Explorer
中,右键单击 References
节点并选择添加 Reference
并找到 System.Drawing.dll
。
.NET Core 3.1 的此问题的答案是只需从 NuGet 安装 System.Drawing.Common。
只需添加到 @feganmeister 答案,您可以使用终端中的
dotnet
CLI 命令安装 NuGet 包:
导航到项目目录并运行以下命令:
cd <project-directory>
dotnet add package System.Drawing.Common