在 Xamarin.Forms 中将图像转换为字节数组

Jon*_*iga 6 c# xamarin xamarin.forms

我需要传递一个Image _thresholdedImage像字节数组这样的图像 ( )...我不知道我怎么能做到这一点。任何的想法?谢谢!

_thresholdedImage.Source = ImageSource.FromStream (() => photo.Source);

var tessResult = await _tesseractApi.SetImage(imageBytes);
Run Code Online (Sandbox Code Playgroud)

Jon*_*iga 2

我无法在 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;
    }
Run Code Online (Sandbox Code Playgroud)