Exemplo n.º 1
0
        private Bots.PhotoSize HandlePhotoSize(IPhotoSize photoSize)
        {
            var photoSizeEmptyTelegram  = photoSize as SharpTelegram.Schema.PhotoSizeEmpty;
            var photoSizeTelegram       = photoSize as SharpTelegram.Schema.PhotoSize;
            var photoCachedSizeTelegram = photoSize as SharpTelegram.Schema.PhotoCachedSize;

            if (photoSizeEmptyTelegram != null)
            {
                return(null);
            }
            else if (photoSizeTelegram != null)
            {
                return(new Bots.PhotoSize
                {
                    Type = photoSizeTelegram.Type,
                    Location = HandleFileLocation(photoSizeTelegram.Location),
                    W = photoSizeTelegram.W,
                    H = photoSizeTelegram.H,
                    Size = (int)photoSizeTelegram.Size
                });
            }
            else if (photoCachedSizeTelegram != null)
            {
                return(new Bots.PhotoCachedSize
                {
                    Type = photoCachedSizeTelegram.Type,
                    Location = HandleFileLocation(photoCachedSizeTelegram.Location),
                    W = photoCachedSizeTelegram.W,
                    H = photoCachedSizeTelegram.H,
                    Bytes = photoCachedSizeTelegram.Bytes
                });
            }

            return(null);
        }
        public static double GetGifDimension(double maxGifDimension, IPhotoSize thumb, IAttributes attributes, bool isWidth)
        {
            TLDocumentAttributeVideo videoAttribute = null;

            if (attributes != null)
            {
                for (var i = 0; i < attributes.Attributes.Count; i++)
                {
                    videoAttribute = attributes.Attributes[i] as TLDocumentAttributeVideo;
                    if (videoAttribute != null)
                    {
                        break;
                    }
                }
            }

            if (videoAttribute != null)
            {
                var width  = videoAttribute.W.Value;
                var height = videoAttribute.H.Value;

                var maxDimension = width;
                if (maxDimension > 0)
                {
                    var scaleFactor = maxGifDimension / maxDimension;

                    return(isWidth ? scaleFactor * width : scaleFactor *height);
                }
            }

            if (thumb != null)
            {
                var width  = thumb.W.Value;
                var height = thumb.H.Value;

                var maxDimension = width;
                if (maxDimension > 0)
                {
                    var scaleFactor = maxGifDimension / maxDimension;

                    return(isWidth ? scaleFactor * width : scaleFactor *height);
                }
            }

            return(maxGifDimension);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var minVerticalRatioToScale = 1.2;
            var scale = 1.2; // must be less than minVerticalRatioToScale to avoid large square photos

            if (string.Equals((string)parameter, "Width", StringComparison.OrdinalIgnoreCase))
            {
                var decryptedMediaPhoto = value as TLDecryptedMessageMediaPhoto;
                if (decryptedMediaPhoto != null)
                {
                    if (decryptedMediaPhoto.H.Value > decryptedMediaPhoto.W.Value)
                    {
                        if (IsScaledVerticalPhoto(minVerticalRatioToScale, decryptedMediaPhoto.H, decryptedMediaPhoto.W))
                        {
                            return(scale * MaxDimension / decryptedMediaPhoto.H.Value * decryptedMediaPhoto.W.Value);
                        }

                        return(MaxDimension / decryptedMediaPhoto.H.Value * decryptedMediaPhoto.W.Value);
                    }

                    return(MaxDimension);
                }

                var mediaPhoto = value as TLMessageMediaPhoto;
                if (mediaPhoto != null)
                {
                    value = mediaPhoto.Photo;
                }

                var photo = value as TLPhoto;
                if (photo != null)
                {
                    IPhotoSize size  = null;
                    var        sizes = photo.Sizes.OfType <IPhotoSize>();
                    foreach (var photoSize in sizes)
                    {
                        if (size == null ||
                            Math.Abs(MaxDimension - size.H.Value) > Math.Abs(MaxDimension - photoSize.H.Value))
                        {
                            size = photoSize;
                        }
                    }

                    if (size != null)
                    {
                        if (size.H.Value > size.W.Value)
                        {
                            if (IsScaledVerticalPhoto(minVerticalRatioToScale, size.H, size.W))
                            {
                                return(scale * MaxDimension / size.H.Value * size.W.Value);
                            }

                            return(MaxDimension / size.H.Value * size.W.Value);
                        }

                        return(MaxDimension);
                    }
                }

                var mediaDocument = value as TLMessageMediaDocument;
                if (mediaDocument != null)
                {
                    return(new VideoToDimensionConverter().Convert(value, targetType, parameter, culture));
                }
            }

            {
                var decryptedMediaPhoto = value as TLDecryptedMessageMediaPhoto;
                if (decryptedMediaPhoto != null)
                {
                    if (decryptedMediaPhoto.H.Value > decryptedMediaPhoto.W.Value)
                    {
                        if (IsScaledVerticalPhoto(minVerticalRatioToScale, decryptedMediaPhoto.H, decryptedMediaPhoto.W))
                        {
                            return(scale * MaxDimension);
                        }

                        return(MaxDimension);
                    }

                    return(MaxDimension / decryptedMediaPhoto.W.Value * decryptedMediaPhoto.H.Value);
                }

                var mediaPhoto = value as TLMessageMediaPhoto;
                if (mediaPhoto != null)
                {
                    value = mediaPhoto.Photo;
                }

                var photo = value as TLPhoto;
                if (photo != null)
                {
                    IPhotoSize size  = null;
                    var        sizes = photo.Sizes.OfType <IPhotoSize>();
                    foreach (var photoSize in sizes)
                    {
                        if (size == null ||
                            Math.Abs(MaxDimension - size.W.Value) > Math.Abs(MaxDimension - photoSize.W.Value))
                        {
                            size = photoSize;
                        }
                    }

                    if (size != null)
                    {
                        if (size.H.Value > size.W.Value)
                        {
                            if (IsScaledVerticalPhoto(minVerticalRatioToScale, size.H, size.W))
                            {
                                return(scale * MaxDimension);
                            }

                            return(MaxDimension);
                        }

                        return(MaxDimension / size.W.Value * size.H.Value);
                    }
                }

                var mediaDocument = value as TLMessageMediaDocument;
                if (mediaDocument != null)
                {
                    return(new VideoToDimensionConverter().Convert(value, targetType, parameter, culture));
                }
            }

            return(double.NaN);
        }
Exemplo n.º 4
0
        public static PhotoSize Parse(IPhotoSize photoSize)
        {
            string key = photoSize.GetType().FullName;

            return(Switch[key](photoSize));
        }