【问题标题】:Why does this sample TIFF import code result in errors?为什么此示例 TIFF 导入代码会导致错误?
【发布时间】:2019-08-31 10:18:51
【问题描述】:

我阅读了How to: Encode and Decode a TIFF Image并复制了代码

// Open a Stream and decode a TIFF image
Stream imageStreamSource = new FileStream("tulipfarm.tif", FileMode.Open, 
FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, 
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];

// Draw the Image
Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);

到 Visual Studio 2017 中的控制台应用程序中。我添加了对 PresentationCore 的引用每个 https://stackoverflow.com/a/50192029/9044571 并允许我添加

using System.Windows.Media.Imaging;

但现在我收到与该行关联的错误(错误 CS0144 无法创建抽象类或接口“图像”的实例)

Image myImage = new Image();

我该如何解决这个问题?问题可能是我从控制台应用程序执行此操作吗?

【问题讨论】:

    标签: c# decode tiff


    【解决方案1】:

    原因是,您实际上是针对System.Drawing.Image class 这是abstract

    解决方法是:-

    1. 删除错误的命名空间

    2. 使用 命名空间别名

    Namespace Aliases

    using 指令也可用于为 命名空间。例如,如果您正在使用以前编写的 包含嵌套命名空间的命名空间,您可能需要声明 一个别名,以提供一种特别引用的速记方式, 如下例:

    using Co = Company.Proj.Nested;  // define an alias to represent a namespace
    
    1. 明确定位正确的目标

      var myImage = new System.Windows.Media.Imaging.Image();

    【讨论】:

    • 谢谢。 var myImage = new System.Windows.Media.Imaging.Image();导致错误 CS0234 命名空间“System.Windows.Media.Imaging”中不存在类型或命名空间名称“Image”(您是否缺少程序集引用?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2012-01-15
    • 2011-01-30
    相关资源
    最近更新 更多