我试图获得图像中的图片集的确切的高度和宽度,使用裁剪图书馆在Image
视图中显示图片/图像位置。
那么,我如何才能得到它的精确的宽度和高度呢?
下面是我的代码
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);
}
根据上面的代码,有什么方法可以得到一个图像的宽度和高度。
发布于 2019-05-19 23:19:52
您可以尝试使用代码这里获取照片的宽度和高度:
在你的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;
}
发布于 2019-05-17 08:34:59
在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}");
https://stackoverflow.com/questions/56186756
复制