asp.net-mvc - 使用 ImageSource.FromUri 从 mvc 检索图像
问题描述
我有发布图像的 mvc web api。我正在尝试使用将图像检索到我的 xamarin 项目中ImageSource.FromUri
我完全能够在网站上查看此图像。但不在我的 xamarin 项目中。
我想知道我做错了什么。这是我的代码
发布图像的 API
[HttpGet]
[AllowAnonymous]
public ActionResult GetImage(string imagePath)
{
var path = Path.Combine(Actions.ImageRootPath, imagePath);
if (!System.IO.File.Exists(path))
return new EmptyResult();
var stream= System.IO.File.OpenRead(path);
return File(stream, "image/png");
}
xamarin xaml
<Frame Style="{StaticResource FrameContainer}" WidthRequest="200" HeightRequest="150" CornerRadius="10" BorderColor="Transparent">
<StackLayout Orientation="Vertical">
<Image Source="{Binding Logo, Converter={StaticResource ImageSource}}" WidthRequest="200" />
<StackLayout HorizontalOptions="CenterAndExpand" Style="{StaticResource Form}" Padding="10,0,0,0" Orientation="Vertical">
<Label Text="{Binding Name}" HorizontalOptions="Center" Style="{StaticResource Header}" />
<Label Text="{Binding TotalVideos}" IsVisible="{Binding TotalVideos, Converter={StaticResource StringNullOrEmpty}}" HorizontalOptions="Center" Style="{StaticResource UnderText}" />
</StackLayout>
</StackLayout>
</Frame>
ImageSource 转换器
if (value is string && value != null && value.ToString().Length >= 4 && !value.ToString().Contains("http"))
{
byte[] Base64Stream = System.Convert.FromBase64String(value.ToString());
var img = ImageSource.FromStream(() => new MemoryStream(Base64Stream));
return img;
}
else if (value is string && !string.IsNullOrEmpty(value?.ToString() ?? "") && value.ToString().Contains("http"))
{
var uri = new Uri(value.ToString());
return ImageSource.FromUri(uri);
//var data = Helper.HttpHelper.GetImage(value.ToString());
//return GetImage(key, data.ToArray());
}
else if (value is byte[])
{
return ImageSource.FromStream(() => new MemoryStream(value as byte[]));
}
return ImageSource.FromFile("NoImage.png");
我尝试过的工作,但我不喜欢
下面的代码可以工作,但我必须先使用 webclient 下载图像
var data = Helper.HttpHelper.GetImage(value.ToString());
return ImageSource.FromStream(() => new MemoryStream(data));
这是图片网址
http://youtubemanager.ddns.net/Youtube.Manager.API/Images/GetImage?imagePath=alen.toma%5Cc2e8bcaa-e258-453b-84c4-868d23b03848.png
解决方案
推荐阅读
- javascript - 如何使用 Joi 验证具有未知键的对象并检查值是否为标量类型?
- bash - 来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:349:启动容器进程导致“exec:\”/bin/bash\”
- reactjs - 简单的反应状态不会在第一次请求时改变
- javascript - pdfjs库的重新缩放问题
- drupal - 在我的 mac 终端上运行 blt vm 命令时,我遇到了 drupal VM 的问题
- node.js - 为什么即使在全局安装 express.js 之后,我仍然无法使用 express.js 运行文件
- sequelize.js - 如何在迁移中通过 sequelize changeColumn 方法更新 mariadb 列的排序规则?
- javascript - 它是一个正确的正则表达式,有空格并且在`some chancter`之间。?
- react-native - 为什么 flatlist 无法放入容器中 - React Native
- apache-spark - 每个 Micro Batch 中的最大偏移量