Skip to content

Instantly share code, notes, and snippets.

@rudyryk
Last active October 6, 2023 11:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rudyryk/6d2fb109d2df6873e207 to your computer and use it in GitHub Desktop.
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
//
// 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;
}
}
}
@huxaiphaer
Copy link

huxaiphaer commented May 20, 2019

Is this method placed in a PCL?, because when I implement it some classes are not resolved such as UIImage , BitmapFactory ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment