gpt4 book ai didi

c# - 为什么这个示例 TIFF 导入代码会导致错误?

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:26 24 4
gpt4 key购买 nike

我读了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 中的控制台应用程序。我根据 https://stackoverflow.com/a/50192029/9044571 添加了对 PresentationCore 的引用这让我可以添加

using System.Windows.Media.Imaging;

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

Image myImage = new Image();

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

最佳答案

原因是,您实际上是针对 System.Drawing.Image 抽象的

修复是:-

  1. 移除错误的命名空间

  2. 使用命名空间别名

Namespace Aliases

The using Directive can also be used to create an alias for a namespace. For example, if you are using a previously written namespace that contains nested namespaces, you might want to declare an alias to provide a shorthand way of referencing one in particular, as in the following example:

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

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

关于c# - 为什么这个示例 TIFF 导入代码会导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com