从System.Drawing.Bitmap创建google.cloud.vision.v1.image

时间:2019-05-17 09:56:16

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

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

var image = Image.FromFile(path);

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

有人可以指出我该课程的文档吗?

2 个答案:

答案 0 :(得分:3)

值得注意的是,您使用的是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()

答案 1 :(得分:0)

从Image.OpenReadstream转换图像

var detect = DetectImageIdentification.DetectSafeSearch(ImageFromStream(Image1.OpenReadStream()));

然后在此处传递

static Image ImageFromStream(Stream stream)
{
    var image = Image.FromStream(stream);
    return image;
}