Am trying to get the exact height and width of a Picture set in an Image, am using cropping library to come out with the Picture/Image place in the Image
view.
So how can I get its exact selected width and height?
Below is how my code
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);
}
Preparing to upload the image ( but I need it's width and height )
// 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);
According to the code above, is there any way I can get the Width and Height of an Image.
In the Android environment, one way to do it is with the BitmapFactory class...
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}");
You can try to use the code here to get the width and and height of the photo:
In your 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);
}
Method to 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.