Example #1
0
        /// <summary>
        /// Returns the proper <see cref="IImageSourceHandler"/> based on the type of <see cref="ImageSource"/> provided.
        /// </summary>
        /// <param name="source">The <see cref="ImageSource"/> to get the handler for.</param>
        /// <returns>The needed handler.</returns>
        private static IImageSourceHandler GetHandler(ImageSource source)
        {
            IImageSourceHandler returnValue = null;

            if (source is UriImageSource)
            {
                #if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP
                returnValue = new UriImageSourceHandler();
                #else
                returnValue = new ImageLoaderSourceHandler();
                #endif
            }
            else if (source is FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (source is StreamImageSource)
            {
                #if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP
                returnValue = new StreamImageSourceHandler();
                #else
                returnValue = new StreamImagesourceHandler();
                #endif
            }
            return(returnValue);
        }
Example #2
0
        private IImageSourceHandler GetHandler(Xamarin.Forms.ImageSource imageSource)
        {
            IImageSourceHandler returnValue = null;

            if (imageSource is Xamarin.Forms.UriImageSource)
            {
                returnValue = new UriImageSourceHandler();
            }
            else if (imageSource is Xamarin.Forms.FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (imageSource is Xamarin.Forms.StreamImageSource)
            {
                returnValue = new StreamImageSourceHandler();
            }
            return(returnValue);
        }
        private static IImageSourceHandler GetHandler(global::Xamarin.Forms.ImageSource source)
        {
            IImageSourceHandler returnValue = null;

            if (source is global::Xamarin.Forms.UriImageSource)
            {
                returnValue = new UriImageSourceHandler();
            }
            else if (source is global::Xamarin.Forms.FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (source is global::Xamarin.Forms.StreamImageSource)
            {
                returnValue = new StreamImageSourceHandler();
            }
            return(returnValue);
        }
        private async Task <BitmapDecoder> GetImageFromImageSource(ImageSource imageSource)
        {
            IImageSourceHandler handler;

            if (imageSource is FileImageSource)
            {
                handler = new FileImageSourceHandler();
            }
            else if (imageSource is StreamImageSource)
            {
                handler = new StreamImageSourceHandler();
                var stream = await((IStreamImageSource)imageSource).GetStreamAsync();
                if (stream != null)
                {
                    return(await BitmapDecoder.CreateAsync(WindowsRuntimeStreamExtensions.AsRandomAccessStream(stream)));
                }
            }
            else if (imageSource is UriImageSource)
            {
                handler = new UriImageSourceHandler();
            }
            else
            {
                throw new NotImplementedException();
            }

            var bitmapImage = await handler.LoadImageAsync(imageSource) as BitmapImage;

            if (bitmapImage?.UriSource != null)
            {
                using (IRandomAccessStreamWithContentType streamWithContent = await RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource).OpenReadAsync())
                {
                    return(await BitmapDecoder.CreateAsync(streamWithContent));
                }
            }

            throw new NotImplementedException();
        }
Example #5
0
        public object ConvertToNative(Brush brush, object context)
        {
            winMedia.Brush winBrush = null;

            // SolidColorBrush
            if (brush is SolidColorBrush)
            {
                SolidColorBrush xamBrush = brush as SolidColorBrush;

                winBrush = new winMedia.SolidColorBrush
                {
                    Color = ConvertColor(xamBrush.Color)
                };
            }

            // LinearGradientBrush
            else if (brush is LinearGradientBrush)
            {
                LinearGradientBrush xamBrush = brush as LinearGradientBrush;

                winBrush = new winMedia.LinearGradientBrush
                {
                    StartPoint   = ConvertPoint(xamBrush.StartPoint),
                    EndPoint     = ConvertPoint(xamBrush.EndPoint),
                    SpreadMethod = ConvertGradientSpread(xamBrush.SpreadMethod)
                };

                foreach (GradientStop xamGradientStop in xamBrush.GradientStops)
                {
                    winMedia.GradientStop winGradientStop = new winMedia.GradientStop
                    {
                        Color  = ConvertColor(xamGradientStop.Color),
                        Offset = xamGradientStop.Offset
                    };

                    (winBrush as winMedia.LinearGradientBrush).GradientStops.Add(winGradientStop);
                }
            }

            else if (brush is ImageBrush)
            {
                ImageBrush xamBrush = brush as ImageBrush;

                winBrush = new winMedia.ImageBrush
                {
                    Stretch    = (winMedia.Stretch)(int) xamBrush.Stretch,
                    AlignmentX = (winMedia.AlignmentX)(int) xamBrush.AlignmentX,
                    AlignmentY = (winMedia.AlignmentY)(int) xamBrush.AlignmentY,
                };

                ImageSource xamImageSource = (brush as ImageBrush).ImageSource;

                if (xamImageSource != null)
                {
                    IImageSourceHandler handler = null;

                    if (xamImageSource.GetType() == typeof(FileImageSource))
                    {
                        handler = new FileImageSourceHandler();
                    }
                    else if (xamImageSource.GetType() == typeof(StreamImageSource))
                    {
                        handler = new StreamImageSourceHandler();
                    }
                    else if (xamImageSource.GetType() == typeof(UriImageSource))
                    {
                        handler = new UriImageSourceHandler();
                    }

                    if (handler != null)
                    {
                        Task <winMedia.ImageSource> winImageSourceTask = handler.LoadImageAsync(xamImageSource);

                        winImageSourceTask.ContinueWith((task) =>
                        {
                            winFound.IAsyncAction asyncAction = winBrush.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                (winBrush as winMedia.ImageBrush).ImageSource = task.Result;
                            });
                        });
                    }
                }
            }

            if (winBrush != null)
            {
                winBrush.Transform = brush.Transform?.GetNativeObject() as winMedia.MatrixTransform;

                // TODO: RelativeTransform and Opacity
            }

            return(winBrush);
        }