谷歌驱动器从内存流上传图像



你好,

我有这样的代码,它将图像从文件上传到谷歌驱动器,一切都很好:

// Create a new file on Google Drive
using (var fsSource = new FileStream(UploadFileName, FileMode.Open, FileAccess.Read))
{
// Create a new file, with metadata and stream.
var request = service.Files.Create(fileMetadata, fsSource, "image/jpg");
request.Fields = "*";
var results = await request.UploadAsync(CancellationToken.None);

}

现在,我想在上传之前进行一些图像处理,这样,如果图像是另一种格式(例如png或bmp(,我就可以将图像转换为jpeg,或者调整图像大小,所以我将文件更改为流式处理,我不想再次在本地保存,因为代码可以在手机上的网站上使用,这就是我保存为流式的原因。

using (MemoryStream ms = new MemoryStream())
{
Image img = Image.FromFile(uploadfileName);
img.Save(ms, ImageFormat.Jpeg);
}

我现在如何将此ms流上传到Google Drive?谢谢你给我任何线索,我不是场上的期望。

感谢大家的帮助。canton7工作组提出的答案是:只需设置ms.Position=0,这样下一次读取就可以从流的开头开始读取,然后在第一个片段中使用它来代替fsSource

最新更新