4

I'm currently using the following code to create an google.cloud.vision.v1.image from a local file

var image = Image.FromFile(path);

In order to save bandwidth and processing time, I need to resize this image before sending it for processing. I can resize a System.Drawing.Bitmap but how can I create google.cloud.vision.v1.image from this Bitmap.

Can someone please point me to the documentation of this class?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
techno
  • 6,100
  • 16
  • 86
  • 192

1 Answers1

3

Worth noting is that you're using the Google.Cloud.Vision.V1.Image's FromFile() method, not .NET's System.Drawing.Image's.

Google's Image class is merely a wrapper that allows you to call their services. It doesn't contain resize methods or the likes.

So you'll have to resize a local image, then write it to a stream in the desired format, then construct Google's Image class from a stream.

That code would look something like this:

System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(path);
System.Drawing.Image resizedImage = ResizeImage(sourceImage, width: 800, height: 600);
System.IO.Stream imageStream = resizedImage.ToStream(ImageFormat.Jpeg);
Google.Cloud.Vision.V1.Image resizedImageToUpload = Google.Cloud.Vision.V1.FromStream(imageStream);

And of course you'd need a using() { ... } or Dispose() here or there.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272