我想将图像读入字节数组。
使用我的xaml,我有一个绑定我的图像:
<Image x:Name="myImageView"
Source="triangles.bmp"
Aspect="AspectFit"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand"/>
我想将myImageView中的数据转换为字节数组。 什么代码可以在xamarin表单上实现 ?
我找到了相反的解决方案(我尚未测试),但我需要这种方式(绑定图像 - &gt; byte [],而不是byte [] - &gt; Image)。
我的用法是,例如,应用模糊卷积滤镜,或找到最大像素值。
感谢。
答案 0 :(得分:0)
由于您使用PCL,您可以通过使用我创建的接口并使用Xamarin DependencyService注册它来实现此目的
我提供了从An URIImageSource &amp;中获取字节的方法。的 FileImageSource 强>
在您的PCL项目中
private void ABCDEFGHIJKLMNOPQRSTUVWXYZ(){
var link = new Uri("https://xamarin.com/content/images/pages/forms/example-app.png");
var goNative = DependencyService.Get<IStackoverflow>();
var bytes = goNative.GetImageBytes(link); // Your Bytes As Requested
}
public interface IStackoverflow // Create A Interface To Access Platform Spefice
{
byte[] GetImageBytes(string filepath);
byte[] GetImageBytes(Uri uri);
}
在您的原生项目中确保您继承上述界面
如果您没有从PCL项目访问它
imagePath = Resources.GetDrawable(Resource.Drawable.triangles.bmp);
public byte[] GetImageBytes(string imagePath)
{
var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close();
return buffer;
}
public byte[] GetImageBytes(Uri uri)
{
var myWebClient = new WebClient();
return myWebClient.DownloadData(uri);
}
如果你不熟悉Xamarin DependencyService,那么就是他们的文档 Xamarin DependencyService