C#.Net从URL下载图像,裁剪并上传,而不保存或显示



我在Web服务器上有大量图像需要裁剪。我想把这个过程自动化。

因此,我的想法是创建一个例程,给定图像的URL,下载图像,裁剪图像,然后将其上传回服务器(作为不同的文件(。我不想在本地保存图像,也不想在屏幕上显示图像。

我已经在C#.Net中有一个项目,我想在其中做这件事,但如果必须的话,我可以做.Net Core。

我环顾四周,但我能找到的下载图像的所有信息都涉及到在本地保存文件,我能发现的关于裁剪的所有信息也涉及到在屏幕上显示图像。

有办法做我需要的事吗?

完全可以向URL发出GET请求,并使用HttpClient.GetByteArrayAsync将响应作为byte[]返回给您。使用该二进制内容,您可以使用Image.FromStream将其读取到Image中。

一旦你有了Image对象,你就可以使用这里的答案来进行裁剪。

//Note: You only want a single HttpClient in your application 
//and re-use it where possible to avoid socket exhaustion issues
using (var httpClient = new HttpClient())
{
//Issue the GET request to a URL and read the response into a 
//stream that can be used to load the image
var imageContent = await httpClient.GetByteArrayAsync("<your image url>");

using (var imageBuffer = new MemoryStream(imageContent))
{
var image = Image.FromStream(imageBuffer);
//Do something with image
}
}

最新更新