Last active
October 6, 2023 11:32
-
-
Save rudyryk/6d2fb109d2df6873e207 to your computer and use it in GitHub Desktop.
C# — Get image size by name in Xamarin.Forms for iOS and Android platforms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ImageMeter.cs | |
// Created by Alexey Kinev on 9 May 2015. | |
// | |
// No Rights Reserved | |
// http://creativecommons.org/publicdomain/zero/1.0/ | |
// | |
// Get image size by name in Xamarin.Forms for iOS and Android platforms. | |
// | |
using System; | |
using Xamarin.Forms; | |
#if __IOS__ | |
using UIKit; | |
#endif | |
#if __ANDROID__ | |
using Android.App; | |
using Android.Graphics; | |
using Android.Content.Res; | |
#endif | |
namespace Project | |
{ | |
public static class ImageMeter | |
{ | |
public static Size GetImageSize(string fileName) | |
{ | |
#if __IOS__ | |
UIImage image = UIImage.FromFile(fileName); | |
return new Size((double)image.Size.Width, (double)image.Size.Height); | |
#endif | |
#if __ANDROID__ | |
var options = new BitmapFactory.Options { | |
InJustDecodeBounds = true | |
}; | |
fileName = fileName.Replace('-', '_').Replace(".png", ""); | |
var resId = Forms.Context.Resources.GetIdentifier( | |
fileName, "drawable", Forms.Context.PackageName); | |
BitmapFactory.DecodeResource( | |
Forms.Context.Resources, resId, options); | |
return new Size((double)options.OutWidth, (double)options.OutHeight); | |
#endif | |
return Size.Zero; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this method placed in a PCL?, because when I implement it some classes are not resolved such as
UIImage
,BitmapFactory
?