簡體   English   中英

C#.Net 從 URL 下載圖像,裁剪和上傳,無需保存或顯示

[英]C#.Net Download Image from URL, Crop, and Upload without Saving or Displaying

我在 Web 服務器上有大量需要裁剪的圖像。 我想自動化這個過程。

所以我的想法是創建一個例程,給定圖像的 URL,下載圖像,裁剪它,然后將其上傳回服務器(作為不同的文件)。 我不想將圖像保存在本地,也不想將圖像顯示到屏幕上。

我已經在 C#.Net 中有一個項目,我想在其中執行此操作,但如果必須,我可以執行.Net Core。

我環顧四周,但我能找到的下載圖像的所有信息都涉及在本地保存文件,而我能找到的所有關於裁剪的信息都涉及將圖像顯示到屏幕上。

有沒有辦法做我需要的?

完全可以向 URL 發出 GET 請求,並使用HttpClient.GetByteArrayAsync將響應作為byte[]返回給您。 使用該二進制內容,您可以使用Image.FromStream將其讀入Image

一旦您擁有該Image object,您就可以使用此處的答案進行裁剪。

//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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM