Exemple #1
0
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     return(Int32Rect.Parse(value));
 }
 /// <summary>Converts a <see cref="T:System.String" /> into a <see cref="T:System.Windows.Int32Rect" />.</summary>
 /// <returns>A new instance of <see cref="T:System.Windows.Int32Rect" /> based on the supplied <paramref name="value" />.</returns>
 /// <param name="value">The string to convert.</param>
 /// <param name="context">Context information that is used for conversion.</param>
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (value != null)
     {
         return(Int32Rect.Parse(value));
     }
     return(base.ConvertFromString(value, context));
 }
Exemple #3
0
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (value == null)
     {
         throw new NotSupportedException("value != null");
     }
     return(Int32Rect.Parse(value));
 }
Exemple #4
0
        /// <summary>Attempts to convert the specified type to an <see cref="T:System.Windows.Int32Rect" />.</summary>
        /// <returns>The <see cref="T:System.Windows.Int32Rect" /> created from converting <paramref name="value" />.</returns>
        /// <param name="context">Provides contextual information required for conversion.</param>
        /// <param name="culture">Cultural information to respect during conversion.</param>
        /// <param name="value">The object being converted.</param>
        /// <exception cref="T:System.NotSupportedException">Thrown if the specified object is NULL or is a type that cannot be converted to an <see cref="T:System.Windows.Int32Rect" />.</exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                throw GetConvertFromException(value);
            }
            string text = value as string;

            if (text != null)
            {
                return(Int32Rect.Parse(text));
            }
            return(base.ConvertFrom(context, culture, value));
        }
Exemple #5
0
 public void ParseNegative()
 {
     Int32Rect.Parse("1, 2, -3, -4");
 }
Exemple #6
0
        public void Parse()
        {
            Int32Rect r = Int32Rect.Parse("1, 2, 3, 4");

            Assert.AreEqual(new Int32Rect(1, 2, 3, 4), r);
        }
Exemple #7
0
        public ErrorCode ReadFrom(Uri requestUri, IEnumerable <KeyValuePair <string, string> > queryNameValuePairs)
        {
            foreach (var item in queryNameValuePairs)
            {
                switch (item.Key)
                {
                case "s":
                {
                    var s = item.Value;
                    if (s.StartsWith("//"))
                    {
                        s = requestUri.Scheme + ":" + s;
                    }
                    if (!Uri.TryCreate(s, UriKind.Absolute, out Source))
                    {
                        return(ErrorCode.SourceUriFormatError);
                    }
                }
                break;

                case "q":
                    int quality;
                    if (!TryConvert(item.Value, out quality))
                    {
                        return(ErrorCode.QualityParseError);
                    }
                    if (!(0 <= quality && quality <= 100))
                    {
                        return(ErrorCode.QualityOutOfRange);
                    }
                    Quality = quality;
                    break;

                case "w":
                    int pixelWidth;
                    if (!TryConvert(item.Value, out pixelWidth))
                    {
                        return(ErrorCode.PixelWidthParseError);
                    }
                    if (pixelWidth < 0)
                    {
                        return(ErrorCode.PixelWidthOutOfRange);
                    }
                    PixelWidth = pixelWidth;
                    break;

                case "h":
                    int pixelHeight;
                    if (!TryConvert(item.Value, out pixelHeight))
                    {
                        return(ErrorCode.PixelHeightParseError);
                    }
                    if (pixelHeight < 0)
                    {
                        return(ErrorCode.PixelHeightOutOfRange);
                    }
                    PixelHeight = pixelHeight;
                    break;

                case "t":
                    ImageType type;
                    if (!Enum.TryParse(item.Value, true, out type))
                    {
                        return(ErrorCode.ImageTypeParseError);
                    }
                    if (type == 0)
                    {
                        return(ErrorCode.ImageTypeOutOfRange);
                    }
                    Type = type;
                    break;

                case "c":
                    if (item.Value == "smart")
                    {
                        CropSmart = true;
                        break;
                    }
                    // the crop rectange is in percentages,
                    // based on the source image dimension
                    // we use this to get the actual crop
                    Int32Rect crop;
                    try
                    {
                        crop = Int32Rect.Parse(item.Value);
                    }
                    catch (Exception)
                    {
                        return(ErrorCode.CropRectParseError);
                    }
                    if (!((0 <= (crop.X + crop.Width)) & (crop.X + crop.Width) <= 100))
                    {
                        return(ErrorCode.CropRectOutOfRange);
                    }
                    if (!((0 <= (crop.Y + crop.Height)) & (crop.Y + crop.Height) <= 100))
                    {
                        return(ErrorCode.CropRectOutOfRange);
                    }
                    Crop = crop;
                    break;

                case "f":
                    FittingType fit;
                    if (!Enum.TryParse(item.Value, true, out fit))
                    {
                        return(ErrorCode.FittingTypeParseError);
                    }
                    Fit = fit;
                    break;

                case "bg-color":
                    try
                    {
                        if (HexPattern.IsMatch(item.Value))
                        {
                            var s = item.Value;
                            if (s.Length == 3)
                            {
                                // shorthand #fc0 -> #ffcc00
                                s = s.Substring(0, 1) + s.Substring(0, 1)
                                    + s.Substring(1, 1) + s.Substring(1, 1)
                                    + s.Substring(2, 1) + s.Substring(2, 1)
                                ;
                            }
                            BackgroundColor = (Color)ColorConverter.ConvertFromString("#" + item.Value);
                        }
                        else
                        {
                            BackgroundColor = (Color)ColorConverter.ConvertFromString(item.Value);
                        }
                    }
                    catch (Exception)
                    {
                        return(ErrorCode.BackgroundColorParseError);
                    }
                    break;

                case "debug":
                {
                    IsDebug = item.Value == "1";
                    break;
                }
                }
            }

            if (Source == null)
            {
                return(ErrorCode.SourceIsRequired);
            }

            return(ErrorCode.None);
        }