【问题标题】:Create google.cloud.vision.v1.image from System.Drawing.Bitmap从 System.Drawing.Bitmap 创建 google.cloud.vision.v1.image
【发布时间】:2019-05-17 09:56:15
【问题描述】:

我目前正在使用以下代码从本地文件创建google.cloud.vision.v1.image

var image = Image.FromFile(path);

为了节省带宽和处理时间,我需要在发送处理之前调整此图像的大小。我可以调整System.Drawing.Bitmap 的大小,但如何从这个位图创建google.cloud.vision.v1.image

有人可以指点我这门课的文档吗?

【问题讨论】:

  • @John Skeet 请帮我解决这个问题...
  • Google.Cloud.Vision.V1.Image.FromStream() 有什么问题?
  • @haim770 我可以直接从System.Drawing.Bitmap 创建它吗?

标签: c# .net google-cloud-vision


【解决方案1】:

值得注意的是,您使用的是Google.Cloud.Vision.V1.ImageFromFile() 方法,而不是.NET 的System.Drawing.Image

Google 的 Image 类只是一个允许您调用其服务的包装器。它不包含调整大小方法等。

所以你必须先resize a local image,然后是write it to a stream in the desired format,然后从流中构造 Google 的 Image 类。

该代码看起来像这样:

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

当然你需要using() { ... }Dispose() 这里或那里。

【讨论】:

  • 谢谢...所以除了创建流没有其他方法吗?
  • 当然,您可以将其写入磁盘。使用FileStream
猜你喜欢
  • 2020-12-03
  • 2014-06-26
  • 2014-01-16
  • 2010-11-10
  • 1970-01-01
  • 2010-09-10
  • 2018-08-31
相关资源
最近更新 更多