C# 将图像转换为Xamarin.Forms中的字节数组
我需要传递一个像字节数组一样的图像(C# 将图像转换为Xamarin.Forms中的字节数组,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我需要传递一个像字节数组一样的图像(image\u thresholdeImage)。。。我不知道该怎么做。有什么想法吗?谢谢大家! _thresholdedImage.Source = ImageSource.FromStream (() => photo.Source); var tessResult = await _tesseractApi.SetImage(imageBytes); 我无法将其转换为X.Forms,而是将以下代码与依赖项服务一起使用。谢谢大家 publ
image\u thresholdeImage
)。。。我不知道该怎么做。有什么想法吗?谢谢大家!
_thresholdedImage.Source = ImageSource.FromStream (() => photo.Source);
var tessResult = await _tesseractApi.SetImage(imageBytes);
我无法将其转换为X.Forms,而是将以下代码与依赖项服务一起使用。谢谢大家
public async Task<byte[]> GetBytesFromImage(string filePath)
{
ConvertImageToBW(filePath);
// Create another bitmap that will hold the results of the filter.
Bitmap thresholdedBitmap = Bitmap.CreateBitmap (BitmapFactory.DecodeFile(filePath));
thresholdedBitmap = BitmapFactory.DecodeFile (thresholdedImagePath);
byte[] bitmapData;
using (var stream = new MemoryStream())
{
thresholdedBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}
公共异步任务GetBytesFromImage(字符串文件路径)
{
ConvertImageToBW(文件路径);
//创建另一个位图以保存过滤器的结果。
位图阈值位图=Bitmap.CreateBitmap(BitmapFactory.DecodeFile(filePath));
thresholdedBitmap=BitmapFactory.DecodeFile(thresholdedImagePath);
字节[]位图数据;
使用(var stream=new MemoryStream())
{
thresholedbitmap.Compress(Bitmap.CompressFormat.Png,0,流);
bitmapData=stream.ToArray();
}
返回位图数据;
}
您是否尝试过使用转换器
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ImageSource retSource = null;
if (value != null)
{
byte[] imageAsBytes = (byte[])value;
retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
}
return retSource;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
您可以使用以下功能:
public async Task<byte[]> Download(string url)
{
using (HttpClient client = new HttpClient())
{
byte[] fileArray = await client.GetByteArrayAsync(url);
return fileArray;
}
}
公共异步任务下载(字符串url)
{
使用(HttpClient=new HttpClient())
{
byte[]fileArray=await client.GetByteArrayAsync(url);
返回文件数组;
}
}
通过在xamairn表单中使用Java.Io,我们可以从imagePath获取字节数组。
它将适用于所有平台
private static byte[] GetBytesFromImage(string imagePath)
{
var imgFile = new File(imagePath);
var stream = new FileInputStream(imgFile);
var bytes = new byte[imgFile.Length()];
stream.Read(bytes);
return bytes;
}
以下代码可用于将图像转换为字节数组。此处CrossMedia插件用于从相机捕获图像
public byte[] imageByte;
Stream imageStream = null;
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{ Name = "pic.jpg"});
if (file == null) return;
imageStream = file.GetStream();
BinaryReader br = new BinaryReader(imageStream);
imageByte = br.ReadBytes((int)imageStream.Length);
事实证明,您可以将MediaFile对象转换为字节数组。因此无需将文件转换为
ImageSource
或Image
var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
byte[] imageArray = null;
using (MemoryStream memory = new MemoryStream()) {
Stream stream = photo.GetStream();
stream.CopyTo(memory);
imageArray = memory.ToArray();
}
来源:在Xamarin论坛上向用户“ajaxer”大声呼喊,在花了3天时间讨论这个问题后,他的解决方案救了我。请指定
SetImage()
的函数签名。假设SetImage()
采用字节[]
,请使用此处提供的答案:。将格式调整为函数期望的格式(可能是位图?)。(我只找到了C++文档)。我的XAMARIN表单不知道Bitmap。我应该包括一些特别的东西吗?我做错了什么?@Gulzar在每个平台上创建您的本机实现。欢迎使用堆栈溢出!虽然这段代码可能会回答这个问题,但最好包含问题的描述,以及您的代码将如何处理给定的问题。对于未来,这里有一些关于堆栈溢出的信息。不要忘记使用
:使用(var br=new BinaryReader(stream))
var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
byte[] imageArray = null;
using (MemoryStream memory = new MemoryStream()) {
Stream stream = photo.GetStream();
stream.CopyTo(memory);
imageArray = memory.ToArray();
}