我有以下小功能:
public Image AddImage(Stream stream)
{
return AddImage(stream as object);
}
我总是收到错误Can not convert from 'object' to 'string'
Stream
是类型System.IO.Stream
怎么了?如何解决这个问题?
从您的更新到问题,您有两个AddImage
. 一个接受 a Stream
,一个接受 a 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;
}