Google drive, upload MemoryStream

This code is working well:

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

But when I try upload a MemoryStream received from System.Drawing.Image, I get an google Exception: 'The given header was not found.'

Code:

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

Can anybody help me fix the code?

Jon Skeet
people
quotationmark

I wouldn't like to say why you're getting that particular error, but after you've called Save, the stream's "cursor" is at the end of the data, so there's no data for Insert to read. You should rewind the stream:

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

I'd also suggest that fileStream is a somewhat misleading name for a MemoryStream variable...

people

See more on this question at Stackoverflow