/// <summary>
 /// Converts a string into a transform.
 /// </summary>
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (!string.IsNullOrEmpty(value))
     {
         UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, value);
         return(BitmapFrame.CreateFromUriOrStream(
                    uriHolder.BaseUri,
                    uriHolder.OriginalUri,
                    null,
                    BitmapCreateOptions.None,
                    BitmapCacheOption.Default,
                    null
                    ));
     }
     return(base.ConvertFromString(value, context));
 }
Esempio n. 2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            try
            {
                if (value == null)
                {
                    throw GetConvertFromException(value);
                }

                if (((value is string) && (!string.IsNullOrEmpty((string)value))) || (value is Uri))
                {
                    UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, value);
                    return(BitmapFrame.CreateFromUriOrStream(
                               uriHolder.BaseUri,
                               uriHolder.OriginalUri,
                               null,
                               BitmapCreateOptions.None,
                               BitmapCacheOption.Default,
                               null
                               ));
                }
                else if (value is byte[])
                {
                    byte[] bytes = (byte[])value;

                    if (bytes != null)
                    {
                        Stream memStream = null;

                        //
                        // this might be a magical OLE thing, try that first.
                        //
                        memStream = GetBitmapStream(bytes);

                        if (memStream == null)
                        {
                            //
                            // guess not.  Try plain memory.
                            //
                            memStream = new MemoryStream(bytes);
                        }

                        return(BitmapFrame.Create(
                                   memStream,
                                   BitmapCreateOptions.None,
                                   BitmapCacheOption.Default
                                   ));
                    }
                }
                else if (value is Stream)
                {
                    Stream stream = (Stream)value;

                    return(BitmapFrame.Create(
                               stream,
                               BitmapCreateOptions.None,
                               BitmapCacheOption.Default
                               ));
                }

                return(base.ConvertFrom(context, culture, value));
            }
            catch (Exception e)
            {
                if (!CriticalExceptions.IsCriticalException(e))
                {
                    if (context == null && CoreAppContextSwitches.OverrideExceptionWithNullReferenceException)
                    {
                        throw new NullReferenceException();
                    }

                    IProvideValueTarget ipvt = context?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                    if (ipvt != null)
                    {
                        IProvidePropertyFallback ippf = ipvt.TargetObject as IProvidePropertyFallback;
                        DependencyProperty       dp   = ipvt.TargetProperty as DependencyProperty;
                        // We only want to call IPPF.SetValue if the target can handle it.
                        // We need to check for non DP scenarios (This is currently an internal interface used
                        //             only by Image so it's okay for now)

                        if (ippf != null && dp != null && ippf.CanProvidePropertyFallback(dp.Name))
                        {
                            return(ippf.ProvidePropertyFallback(dp.Name, e));
                        }
                    }
                }

                // We want to rethrow the exception in the case we can't handle it.
                throw;
            }
        }