Пример #1
0
        public void UpdateImageContainer(string url)
        {
            var image = ImageProxyFile.Parse(url);

            if (image == null)
            {
                _imageContainer.Content = "";
            }
            else
            {
                image.Width             = 230;
                image.Height            = 129;
                _imageContainer.Content = LINQPad.Util.Image(image.ToString());
            }
        }
Пример #2
0
        public static ImageProxyFile Parse(string url)
        {
            var image = new ImageProxyFile();

            var match = Pattern.Match(url);

            if (!match.Success)
            {
                return(null);
            }

            image.Endpoint = match.Groups[EndpointIndex].Value;
            image.Hash     = match.Groups[HashIndex].Value;
            var width  = match.Groups[WidthIndex];
            var height = match.Groups[HeightIndex];
            var crop   = match.Groups[CropIndex];
            var color  = match.Groups[ColorIndex];
            var type   = match.Groups[TypeIndex];

            if (width.Success)
            {
                image.Width = Convert.ToInt32(width.Value, CultureInfo.InvariantCulture);
            }

            if (height.Success)
            {
                image.Height = Convert.ToInt32(height.Value, CultureInfo.InvariantCulture);
            }

            if (crop.Success)
            {
                var c = crop.Captures;
                for (int i = 0; i < c.Count; i++)
                {
                    switch (c[i].Value)
                    {
                    case "face":
                    {
                        image.Crop |= ImageCrop.Face;
                        break;
                    }

                    case "hint":
                    {
                        image.Crop |= ImageCrop.Hint;
                        break;
                    }

                    default:
                        throw new InvalidOperationException("unhandled crop parameter");
                    }
                }
            }

            if (color.Success)
            {
                image.Color = color.Value;
            }

            // required

            switch (type.Value)
            {
            case "jpg":
            {
                image.OutputType = ImageType.Jpg;
                break;
            }

            case "png":
            {
                image.OutputType = ImageType.Png;
                break;
            }

            case "webp":
            {
                image.OutputType = ImageType.Webp;
                break;
            }

            default:
            {
                throw new InvalidOperationException("unhandled type parameter");
            }
            }

            return(image);
        }