2015-01-13 157 views
0

此代码运行良好:谷歌驱动器,上传的MemoryStream

img.Save("111.png", System.Drawing.Imaging.ImageFormat.Png); 
using (MemoryStream fileStream = new MemoryStream(System.IO.File.ReadAllBytes("111.png"))) 
{ 
    FilesResource.InsertMediaUpload request = _drive.Files.Insert(item, fileStream, mimeType); 
    request.Upload(); 
} 

但是当我尝试上传System.Drawing.Image收到MemoryStream,我得到一个谷歌例外: 'The given header was not found.'

代码:

System.Drawing.Image img = myImgObj; 
using (MemoryStream fileStream = new MemoryStream())) 
{   
    img.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png); 
    FilesResource.InsertMediaUpload request = _drive.Files.Insert(item, fileStream, mimeType); 
    request.Upload(); 
} 

任何人都可以帮我修复代码吗?

回答

1

我不想说为什么你得到特定的错误,但你叫Save,流的“光标”之后是在年底的数据,所以没有数据Insert到读。你应该快退流:

img.Save(...); 
fileStream.Position = 0; 

我还建议fileStream是有点误导名称为MemoryStream变量...