internal static ImageStream GetImage(MediaFileImage mediaAssetImage, MediaFileGetImageOptions options)
        {
            var w = options.Width;
            var h = options.Height;

            Bitmap img;

            if (w == h && w > 0 && w <= 96 && h <= 96 && options.ResizeAspect == MediaFileGetImageOptions.ImageResizeAspect.AspectFill)
            {
                img = MediaStore.Images.Thumbnails.GetThumbnail(MediaFileManager.Context.ContentResolver, mediaAssetImage.Id, ThumbnailKind.MicroKind, null);
            }
            else if (w > 0 && w <= 512 && h > 0 && h <= 384)
            {
                img = MediaStore.Images.Thumbnails.GetThumbnail(MediaFileManager.Context.ContentResolver, mediaAssetImage.Id, ThumbnailKind.MiniKind, null);
            }
            else
            {
                img = BitmapFactory.DecodeFile(mediaAssetImage.Uri);
            }

            using (var finalImg = ApplyImageOptions(img, mediaAssetImage.Orientation, options, w, h))
                using (var stream = new MemoryStream())
                {
                    finalImg.Compress(Bitmap.CompressFormat.Jpeg, options.Quality, stream);
                    stream.Position = 0;

                    return(ImageStream.FromStream(stream, finalImg.Width, finalImg.Height));
                }
        }
        internal static ImageStream GetVideoImage(MediaFileVideo mediaAssetVideo, MediaFileGetImageOptions options)
        {
            var w = options.Width;
            var h = options.Height;

            Bitmap img;

            if (w == h && w > 0 && w <= 96 && h <= 96 && options.ResizeAspect == MediaFileGetImageOptions.ImageResizeAspect.AspectFill)
            {
                img = MediaStore.Video.Thumbnails.GetThumbnail(MediaFileManager.Context.ContentResolver, mediaAssetVideo.Id, VideoThumbnailKind.MicroKind, null);
            }
            else if ((w <= 512 && h <= 384) || w <= 0 || h <= 0)
            {
                img = MediaStore.Video.Thumbnails.GetThumbnail(MediaFileManager.Context.ContentResolver, mediaAssetVideo.Id, VideoThumbnailKind.MiniKind, null);
            }
            else
            {
                img = Android.Media.ThumbnailUtils.CreateVideoThumbnail(mediaAssetVideo.Uri, ThumbnailKind.FullScreenKind);
            }

            using (var finalImg = ApplyImageOptions(img, MediaFileImageOrientation.Up, options, w, h))
                using (var stream = new MemoryStream())
                {
                    finalImg.Compress(Bitmap.CompressFormat.Jpeg, options.Quality, stream);
                    stream.Position = 0;

                    return(ImageStream.FromStream(stream, finalImg.Width, finalImg.Height));
                }
        }
Example #3
0
        internal static ImageStream GetImage(PHAsset asset, MediaFileGetImageOptions options)
        {
            nfloat w = options.Width;
            nfloat h = options.Height;

            switch (options.Orientation)
            {
            case MediaFileImageOrientation.Left:
            case MediaFileImageOrientation.LeftMirrored:
            case MediaFileImageOrientation.Right:
            case MediaFileImageOrientation.RightMirrored:
                var wT = w;
                w = h;
                h = wT;
                break;
            }

            if (w <= 0)
            {
                if (h > 0)
                {
                    w = asset.PixelWidth * h / asset.PixelHeight;
                }
                else
                {
                    w = asset.PixelWidth;
                }
            }

            if (h <= 0)
            {
                h = asset.PixelHeight * w / asset.PixelWidth;
            }

            ImageStream image = null;

            PHImageManager.DefaultManager.RequestImageForAsset(
                asset,
                new CoreGraphics.CGSize(w, h),
                ImageResizeAspectToPH(options.ResizeAspect),
                new PHImageRequestOptions {
                Synchronous = true, ResizeMode = PHImageRequestOptionsResizeMode.Exact
            },
                (requestedImage, info) =>
            {
                if (options.Orientation != MediaFileImageOrientation.Up ||
                    requestedImage.Size.Width != w ||
                    requestedImage.Size.Height != h)
                {
                    var destW = w;
                    var destH = h;

                    if (options.ResizeAspect == MediaFileGetImageOptions.ImageResizeAspect.AspectFit)
                    {
                        var widthScale  = w / requestedImage.Size.Width;
                        var heightScale = h / requestedImage.Size.Height;
                        var scale       = (nfloat)Math.Min(widthScale, heightScale);

                        switch (options.Orientation)
                        {
                        case MediaFileImageOrientation.Left:
                        case MediaFileImageOrientation.LeftMirrored:
                        case MediaFileImageOrientation.Right:
                        case MediaFileImageOrientation.RightMirrored:
                            h     = requestedImage.Size.Width * scale;
                            w     = requestedImage.Size.Height * scale;
                            destW = h;
                            destH = w;
                            break;

                        default:
                            w     = requestedImage.Size.Width * scale;
                            h     = requestedImage.Size.Height * scale;
                            destW = w;
                            destH = h;
                            break;
                        }
                    }
                    else
                    {
                        switch (options.Orientation)
                        {
                        case MediaFileImageOrientation.Left:
                        case MediaFileImageOrientation.LeftMirrored:
                        case MediaFileImageOrientation.Right:
                        case MediaFileImageOrientation.RightMirrored:
                            var wT = w;
                            w      = h;
                            h      = wT;
                            break;
                        }
                    }

                    var cg          = requestedImage.CGImage;
                    int bytesPerRow = (int)w * 4;
                    var ctx         = new CoreGraphics.CGBitmapContext(null, (int)w, (int)h, 8, bytesPerRow, cg.ColorSpace, CoreGraphics.CGImageAlphaInfo.PremultipliedLast);

                    Func <float, float> radians = (degrees) =>
                    {
                        return(degrees * ((float)Math.PI / 180f));
                    };

                    switch (options.Orientation)
                    {
                    case MediaFileImageOrientation.UpMirrored:
                        ctx.TranslateCTM(w, 0);
                        ctx.ScaleCTM(-1f, 1f);
                        break;

                    case MediaFileImageOrientation.Down:
                        ctx.TranslateCTM(w, h);
                        ctx.RotateCTM(radians(180f));
                        break;

                    case MediaFileImageOrientation.DownMirrored:
                        ctx.TranslateCTM(0, h);
                        ctx.RotateCTM(radians(180f));
                        ctx.ScaleCTM(-1f, 1f);
                        break;

                    case MediaFileImageOrientation.Left:
                        ctx.TranslateCTM(w, 0);
                        ctx.RotateCTM(radians(90f));
                        break;

                    case MediaFileImageOrientation.LeftMirrored:
                        ctx.TranslateCTM(w, h);
                        ctx.RotateCTM(radians(270f));
                        ctx.ScaleCTM(1f, -1f);
                        break;

                    case MediaFileImageOrientation.Right:
                        ctx.TranslateCTM(0, h);
                        ctx.RotateCTM(radians(270f));
                        break;

                    case MediaFileImageOrientation.RightMirrored:
                        ctx.TranslateCTM(0, 0);
                        ctx.RotateCTM(radians(90f));
                        ctx.ScaleCTM(1f, -1f);
                        break;

                    default:
                        break;
                    }

                    ctx.DrawImage(new CoreGraphics.CGRect(0, 0, destW, destH), cg);

                    requestedImage = UIImage.FromImage(ctx.ToImage());

                    ctx.Dispose();
                }

                using (var stream = requestedImage.AsJPEG(options.Quality / 100f).AsStream())
                {
                    image = ImageStream.FromStream(stream, (int)requestedImage.Size.Width, (int)requestedImage.Size.Height);
                }
                requestedImage.Dispose();
            });

            return(image);
        }