将来自 Xam.Plugin.Media 5.0.1 的图像源转换为 Xamarin Forms 中的字节数组?
Convert imageSource coming from Xam.Plugin.Media 5.0.1 to byte array in Xamarinforms?
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await Application.Current.MainPage.DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
});
if (file == null)
return;
var tmpSrc = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
ImageSource toBeConverted = tmpSrc;
- 我希望将变量 toBeConverted 转换为 Byte[] 所以
我可以将它发送到我的 webapi ...
ImageSource
是一种为 Xamarin.Forms.Image 提供源图像以显示某些内容的方法。如果你已经在屏幕上显示了一些东西,你的 Image
视图填充了来自其他地方的数据,例如文件或资源或存储在内存中的数组中......或者你在第一名。与其尝试从 ImageSource
取回该数据,不如保留对它的引用并根据需要上传。
所以你可以在选择照片后从文件中获取字节数组。
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
});
if (file == null)
return;
var bytes = File.ReadAllBytes(file.Path); // you could get the byte[] here from the file path.
这段代码对我也有用...
private async void Capture()
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await Application.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Test",
SaveToAlbum = true,
CompressionQuality = 75,
CustomPhotoSize = 50,
PhotoSize = PhotoSize.Medium,
DefaultCamera = CameraDevice.Front
});
if (file == null)
return;
var stream = file.GetStream();
if (stream != null)
{
var StreamByte = ReadAllBytes(stream);
var NewStream = new MemoryStream(StreamByte);
// stream = mystream;
Device.BeginInvokeOnMainThread(() => {
ImageSource = ImageSource.FromStream(() => NewStream);
});
student.ProfilePicture = StreamByte;
}
}
public byte[] ReadAllBytes(Stream instream)
{
if (instream is MemoryStream)
return ((MemoryStream)instream).ToArray();
using (var memoryStream = new MemoryStream())
{
instream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await Application.Current.MainPage.DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
});
if (file == null)
return;
var tmpSrc = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
ImageSource toBeConverted = tmpSrc;
- 我希望将变量 toBeConverted 转换为 Byte[] 所以 我可以将它发送到我的 webapi ...
ImageSource
是一种为 Xamarin.Forms.Image 提供源图像以显示某些内容的方法。如果你已经在屏幕上显示了一些东西,你的 Image
视图填充了来自其他地方的数据,例如文件或资源或存储在内存中的数组中......或者你在第一名。与其尝试从 ImageSource
取回该数据,不如保留对它的引用并根据需要上传。
所以你可以在选择照片后从文件中获取字节数组。
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
});
if (file == null)
return;
var bytes = File.ReadAllBytes(file.Path); // you could get the byte[] here from the file path.
这段代码对我也有用...
private async void Capture() { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await Application.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "OK"); return; } var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Test", SaveToAlbum = true, CompressionQuality = 75, CustomPhotoSize = 50, PhotoSize = PhotoSize.Medium, DefaultCamera = CameraDevice.Front }); if (file == null) return; var stream = file.GetStream(); if (stream != null) { var StreamByte = ReadAllBytes(stream); var NewStream = new MemoryStream(StreamByte); // stream = mystream; Device.BeginInvokeOnMainThread(() => { ImageSource = ImageSource.FromStream(() => NewStream); }); student.ProfilePicture = StreamByte; } } public byte[] ReadAllBytes(Stream instream) { if (instream is MemoryStream) return ((MemoryStream)instream).ToArray(); using (var memoryStream = new MemoryStream()) { instream.CopyTo(memoryStream); return memoryStream.ToArray(); } }