Google驱动,上传MemoryStream

Google drive, upload 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 时,我得到一个 google 异常: '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();
}

谁能帮我修改代码?

我不想说为什么你会收到那个特定的错误,但在你调用 Save 之后,流的 "cursor" 位于 末尾 的数据,所以没有数据供 Insert 读取。您应该倒回直播:

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

我还建议 fileStream 对于 MemoryStream 变量来说是一个有点误导的名称...