首页 > 解决方案 > 如何在.net c#中从API反序列化图像

问题描述

我想通过 API 调用将图像反序列化为 .net。在项目的 API 中,URI 使用当前人的 Id 扩展(作为字符串查询),然后使用该完整 URI,人的图像会显示在 Postman 中。所以,我需要这个 URI 的数据才能在搜索 UI 上显示每个人的图像。

在调试时,API 调用似乎出了点问题。那里没有数据,变量 img 为空。我正在传递这个捕获异常的方法,因为没有数据。

这个电话有什么问题?(img 为空,字节数组为空等)

这是我的 APIHelper 类的方法。

public byte[] GetFaceImageByte(string faceId)
    
{ 
        
   var cancellationTokenSource = new CancellationTokenSource();

   var img =  restClient.ExecuteTaskAsync<byte[]>(new RestRequest(
$"/facevis/faces/{faceId}/image"), cancellationTokenSource.Token);
                       
   return img.Result.RawBytes;
}

这是我将显示图像的另一个类的方法。

private static BitmapImage LoadBitmapImageResource(Guid resourceName)
    
{
        
   try
        
   {
            
      APIHelper apiHelper = new APIHelper();
            
            
      var imgData = apiHelper.GetFaceImageByte(resourceName.ToString());
    
          
      using var mStream = new MemoryStream(imgData);
                
         var img = new BitmapImage();
                
         img.BeginInit();
                
         img.StreamSource = mStream;
                
         img.EndInit();
                
         return img;
        
   }
        
   catch
        
   {
            
         return null;
        
   }

}

标签: c#jsonimagerest

解决方案


推荐阅读