How to get the image file size of an image in a mono-point without downloading it?

How to get the image size of an image file in a single point without loading it into memory?

I tried using CGImageSource from the MonoTouch.ImageIO namespace, as stated in the Apple documentation and in this blog post:

oleb.net

But this code does not work:

public Size GetImageSizeFromImageFile (string image_file_path)
    {

        CGImageSource imageSource = CGImageSource.FromUrl (NSUrl.FromFilename (image_file_path));

        if (imageSource == null) {
            // Error loading image
            Console.WriteLine ("Error loading image info for file: " + image_file_path);
            return Size.Empty;
        }

        NSDictionary imageProperties = new NSDictionary ();
        imageSource.CopyProperties (imageProperties, 0);

        int width = (int)imageProperties.ValueForKey (new NSString ("kCGImagePropertyPixelWidth"));
        int height = (int)imageProperties.ValueForKey (new NSString ("kCGImagePropertyPixelHeight"));


        Console.WriteLine (@"Image " + image_file_path + " dimensions: " + width + " x " + height+" px");

        imageProperties.Dispose ();

        return new Size(width, height);
    }

Casting to strings does not matter; the field returns empty.

Any help is appreciated, thanks!

+3
source share
2 answers

Recent versions of MonoTouch make it much easier to get image properties using the new method GetProperties. For instance.

using (var src = CGImageSource.FromUrl (NSUrl.FromFilename (filename))) {
    CGImageOptions options = new CGImageOptions () { ShouldCache = false };
    // do not forget the '0' image index or you won't get what you're looking for!
    using (var p = src.GetProperties (options, 0)) {
        Console.WriteLine ("{0}x{1}", p.PixelWidth, p.PixelHeight);
    }
}
+4
source

, Mono Touch. XCode Mono Touch, XCode.

Mono Touch imageSource.CopyProperties(imageProperties,0) imageProperties.Keys Count 0

, bugzilla.xamarin.com/

0

All Articles