获取错误:无法从“对象”转换为“字符串”

时间:2013-01-11 13:00:48

标签: c# asp.net

我有以下小功能:

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

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

StreamSystem.IO.Stream

的类型

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

2 个答案:

答案 0 :(得分:1)

从您的更新到问题,您有两个AddImage重载。一个采用Stream,一个采用string

您正在尝试呼叫AddImage(object),但没有匹配的重载。 编译器告诉您不能将对象发送到AddImage(string blah)方法。

答案 1 :(得分:0)

为什么不创建一个功能

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;
    }