在图像视图中裁剪后,如何获取从图库中选取的图像的宽度和高度

时间:2019-05-17 12:48:38

标签: c# image xamarin xamarin.forms

我试图获取图片中图片集的准确高度宽度,正在使用裁剪library来生成图片/图片放在Image视图中。

那我怎样才能得到确切的宽度和高度呢?

下面是我的代码

  1. 从图库中选择图片,然后裁剪。
private void SelectImageFromGallery(object sender, EventArgs e)
                {
                    new ImageCropper
                    {
                        CropShape = ImageCropper.CropShapeType.Rectangle,
                        Success = imageFile =>
                        {
                            Device.BeginInvokeOnMainThread(() =>
                        {
                            profile_img.Source = ImageSource.FromFile(imageFile);
                            var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                            var pathFile = profile_img.Source
                                .ToString().Replace("/data/user/0", "/data/data");
                            File.Copy(imageFile, Path.Combine(folderPath, Path.GetFileName(pathFile)));

                        });
                    }
                }.Show(this);

            }
  1. 准备上传图片(但我需要它的宽度和高度

    // Get the folder path of the cropped Image
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);         
    
    // Convert it into bytes.
    var readAllBytes = File.ReadAllBytes(Path.Combine(folderPath, getPathName));
    // ByteArrayContent.
    var baContent = new ByteArrayContent(readAllBytes);
    

根据上面的代码,有什么方法可以获取图像的 Width Height

2 个答案:

答案 0 :(得分:1)

在Android环境中,一种方法是使用BitmapFactory类...

var options = new Android.Graphics.BitmapFactory.Options { InJustDecodeBounds = true };
Android.Graphics.BitmapFactory.DecodeFile(FULL_FILE_PATH_HERE, options);
Android.Util.Log.Info("MyApp", $"Width ={options.OutWidth}");
Android.Util.Log.Info("MyApp", $"Height={options.OutHeight}");

答案 1 :(得分:1)

您可以尝试使用代码here来获取照片的宽度和高度:

在您的SelectImageFromGallery中:

private async void SelectImageFromGallery(object sender, EventArgs e)
        {
            new ImageCropper
            {
                CropShape = ImageCropper.CropShapeType.Rectangle,
                Success = imageFile =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {

                        ...

                        Size s = GetImageSize(imageFile);
                        Console.WriteLine(s);
                    });
                }
            }.Show(this);

        }

GetImageSize的方法:

 public static Size GetImageSize(string fileName)
        {

            if (Device.RuntimePlatform == Device.iOS)
            {
                UIImage image = UIImage.FromFile(fileName);
                return new Size((double)image.Size.Width, (double)image.Size.Height);
            }
            else if (Device.RuntimePlatform == Device.Android)
            {
                var options = new BitmapFactory.Options
                {
                    InJustDecodeBounds = true
                };
                fileName = fileName.Replace('-', '_').Replace(".png", "");
                var resId = Android.App.Application.Context.Resources.GetIdentifier(
                    fileName, "drawable", Android.App.Application.Context.PackageName);
                BitmapFactory.DecodeResource(
                    Android.App.Application.Context.Resources, resId, options);
                return new Size((double)options.OutWidth, (double)options.OutHeight);
            }

            return Size.Zero;
        }