获取错误:不能转换'object'& # 39;字符串# 39;

本文关键字:不能 字符串 转换 object 取错误 获取 | 更新日期: 2023-09-27 17:52:45

我有以下小函数:

public Image AddImage(Stream stream)
{
    return AddImage(stream as object);
}

我总是得到错误Can not convert from 'object' to 'string'

StreamSystem.IO.Stream类型

什么错了吗?如何解决这个问题?

获取错误:不能转换'object'& # 39;字符串# 39;

从您对问题的更新来看,您有两个AddImage过载。一个取Stream,一个取string

您正在尝试调用AddImage(object),但没有匹配的过载。编译器告诉你不能将对象发送给AddImage(string blah)方法。

为什么不只创建一个函数

public Image AddImage(Stream stream, string filename)
    {
        string contentType = "";
        Image image; 
        // The extension this file has will be taken to be its format.
        switch (Path.GetExtension(filename))
        {
            case ".tiff": contentType = "image/tif"; break;
            case ".tif": contentType = "image/tif"; break;
            case ".png": contentType = "image/png"; break;
            case ".bmp": contentType = "image/png"; break;
            case ".gif": contentType = "image/gif"; break;
            case ".jpg": contentType = "image/jpg"; break;
            case ".jpeg": contentType = "image/jpeg"; break;
            default: contentType = "image/jpg"; break;
        }
        //What you do in the third function;
       return image;
    }