温馨提示×

C# Picture如何从URL加载图片

c#
小樊
165
2024-07-10 00:35:21
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

您可以使用System.Net.WebClient类来从URL加载图片。以下是一个示例代码:

using System;
using System.Net;
using System.Drawing;

class Program
{
    static void Main()
    {
        string imageUrl = "https://example.com/image.jpg";
        
        using (WebClient webClient = new WebClient())
        {
            byte[] imageData = webClient.DownloadData(imageUrl);
            Image image;
            
            using (var ms = new System.IO.MemoryStream(imageData))
            {
                image = Image.FromStream(ms);
            }
            
            image.Save("downloadedImage.jpg");
            
            Console.WriteLine("Image downloaded and saved!");
        }
    }
}

请注意,您需要使用System.Drawing命名空间中的Image类来处理图像。在上面的示例中,我们首先使用WebClient下载图像数据,然后将其转换为Image对象,并最终保存到本地文件。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:C# Picture如何加载图片

0