Сначала я попробовал этот метод, который нашел в этом посте, но у меня это не сработало, потому что он не мог преобразовать FileImageSource в StreamImageSource.
Код: Выделить всё
public static async Task ImageSourceToByteArrayAsync(ImageSource imageSource)
{
Stream stream = await ((StreamImageSource)imageSource).Stream(CancellationToken.None);
byte[] bytesAvailable = new byte[stream.Length];
stream.Read(bytesAvailable, 0, bytesAvailable.Length);
return bytesAvailable;
}
После этого я попытался использовать эту функцию:
Код: Выделить всё
public static async Task FileImageSourceToByteArrayAsync(FileImageSource fileImageSource)
{
// Get the file path from the FileImageSource
var filePath = fileImageSource.File;
// Check if the file exists
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"The file '{filePath}' does not exist.");
}
// Read the file into a byte array
return await File.ReadAllBytesAsync(filePath);
}
Поэтому я ищу конкретный ответ, как прочитать локальное изображение из папки ресурсов и преобразовать его в byte[], который я смогу сохранить и скрыть позже. на изображение, чтобы отобразить его.
Подробнее здесь: https://stackoverflow.com/questions/769 ... ce-to-byte
Мобильная версия