我正在尝试获取图像中设置的图片的确切高度和宽度,我正在使用裁剪库在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);
}
准备上传图片(但我需要它的宽度和高度)
// 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);
根据上面的代码,有什么方法可以获得图像的宽度和高度。
您可以尝试使用这里的代码来获取照片的宽度和高度:
在您的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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句