【问题标题】:Where is the Image class in C#?C# 中的 Image 类在哪里?
【发布时间】:2020-06-05 06:40:33
【问题描述】:

我只是想对位图图像进行编码和解码,但 Image 类显然不存在。我要离开this page 来创建一个解码器。我在

处遇到错误

Image drawnImg = new Image();

它说,“找不到类型或命名空间”,我使用的每个导入都没有修复这个错误。我在这里错过了什么图书馆?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace Thompson_BmpDecodeEncode
{
    class Thompson_BmpDecodeEndoce
    {
        static void Main(string[] args)
        {

            Uri img = new Uri("<FilePath to image.bmp>", UriKind.RelativeOrAbsolute);
            BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(img, 
                BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource2 = decoder2.Frames[0];

            Image drawnImg = new Image();

            Console.ReadKey();
       }
    }
}

【问题讨论】:

  • 你试过去你最喜欢的搜索引擎并输入c# image class吗?这就是你要找的东西吗?
  • 点击Project --> Add Reference,然后选择左侧的Framework。在右侧找到 System.Drawing 并在其旁边打勾。点击确定。
  • 您的目标是什么:Winforms、WPF、ASP..?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它!

标签: c# bitmap


【解决方案1】:

当我找不到库时,我喜欢做的一件事是打开我最喜欢的搜索引擎并搜索它(添加 c# 也无妨)。

搜索“c# image class”时,最上面的结果是:Image Class,其中写着:

命名空间:
System.Drawing


更新:

另外,为了为库添加using 语句,您需要首先在项目中添加对该库的引用。在Visual Studio中,可以在Solution Explorer中的项目下右键References节点,然后在Assemblies -&gt; Framework下,可以勾选System.Drawing旁边的框:

如果您没有使用 Visual Studio,您可以编辑您的 .csproj 文件并参考:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="" DefaultTargets="" xmlns="">
  <ItemGroup>
    <Reference Include="System.Drawing" />

【讨论】:

  • 在我问这个之前我已经搜索过了。我试过这个,它给了我同样的错误。我所有的重构都建议使用静态 System.Net.Mime.MediaTypeNames;我不确定这是否是正确的对象。
  • 我在您的代码示例中没有看到 using System.Drawing;“我试过这个,它给了我同样的错误”是什么意思。您具体尝试了什么,具体是什么错误?
  • 我删除了它,因为它不起作用。我会加回来的。
  • “没有用”是什么意思?请包括您收到的任何错误消息(还包括您如何为您的项目添加对该库的引用,这是在添加 using 语句之前所必需的)
  • 哦,我的错 - 我没有意识到你正在使用 WPF。在the paged you linked 的底部,有一个部分说:“另请参阅:Imaging Overview,并在该页面上声明,“图像位于 System.Windows.Controls 命名空间中。”
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
相关资源
最近更新 更多