Click here to Skip to main content
15,036,441 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Good day,

I have this code which upload an image to Google drive from file, everything works well:
// 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);

Now I want to do some image manipulation before uploading so that I could convert the image to jpeg if the image is in another format (png or bmp for example) or resize the image, so I changed the file to stream for manipulation, I don't want to save it locally again because the code could be used on a website on mobiles, that's why I am saving to stream.

What I have tried:

<pre lang="C#">
using (MemoryStream ms = new MemoryStream())

                Image img = Image.FromFile(uploadfileName);
                img.Save(ms, ImageFormat.Jpeg);

How can I now upload this ms stream to Google Drive? Thanks for any clue, I'm not an expect in field.
Updated 25-Jun-21 3:17am

1 solution

Got the answer:

Just set ms.Position = 0, so that the next read starts reading from the beginning of the stream, then use it in place of your fsSource.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900