demo_trade/Utils/ImageHelper.cs

38 lines
1.1 KiB
C#
Raw Permalink Normal View History

2024-10-04 12:41:04 +00:00
using Avalonia.Media.Imaging;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace demo_trade.Utils
{
public static class ImageHelper
{
public static string BASE_URL = "http://localhost:5025/images";
public static async Task<Bitmap?> LoadFromRemoteByName(string imageName)
{
if (String.IsNullOrWhiteSpace(imageName)) imageName = "default.png";
using var httpClient = new HttpClient();
try
{
var response = await httpClient.GetAsync(BASE_URL + "/" + imageName);
var data = await response.Content.ReadAsByteArrayAsync();
var bimap = new Bitmap(new MemoryStream(data));
bimap.Save(imageName);
return new Bitmap(new MemoryStream(data));
}
catch (Exception ex)
{
return null;
}
}
}
}