c# - 如何在.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;
}
}
解决方案
推荐阅读
- windows - windows Jenkins上的groovy命令curl
- hyperledger-fabric - 在 Hyperledger Fabric 中,客户端是向所有订单发送交易还是仅向一个订购者节点发送交易?
- c# - 无法将 int 类型隐式转换为 (namespace.model.class) 类型
- python - 如何用另一个数据框中最接近的日期填充一个数据框中的日期列
- google-sheets - 提取根域以及 Google 表格中的 TLD regexextract
- model-binding - 在模型绑定上处理 Nullable 引用和 RequiredAttribute 的正确方法
- c++ - 使用 Qt Creator 在调试模式下编译一段代码
- html - 自动调整和对齐 td
- python - 带有python脚本的pdf-latex,自定义python变量到latex输出
- c# - Selenium 中与 WebElement 交互的最佳等待策略是什么