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 static IImageSourceHandler GetHandler(ImageSource source)
        {
            IImageSourceHandler returnValue = null;

            //if (source is UriImageSource)
            //{
            //    returnValue = new ImageLoaderSourceHandler();
            //}
            //else
            if (source is FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (source is 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 #6
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);
        }
        // private BitmapImage bitmapImage;

        protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
            {
                return;
            }



            var min = Math.Min(Element.Width, Element.Height);

            if (min / 2.0f <= 0)
            {
                return;
            }

            try
            {
                Control.Width  = min;
                Control.Height = min;

                Control.Width  = min;
                Control.Height = min;

                // Fill background color
                Control.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));

                // Fill stroke
                Control.Stroke = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));

                Control.StrokeThickness = 0.1;

                var force = e.PropertyName == VisualElement.XProperty.PropertyName ||
                            e.PropertyName == VisualElement.YProperty.PropertyName ||
                            e.PropertyName == VisualElement.WidthProperty.PropertyName ||
                            e.PropertyName == VisualElement.HeightProperty.PropertyName ||
                            e.PropertyName == VisualElement.ScaleProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorXProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorYProperty.PropertyName;


                //already set
                if (file == Element.Source && !force)
                {
                    return;
                }

                file = Element.Source;

                BitmapImage bitmapImage = null;

                // Handle file images
                if (file is FileImageSource)
                {
                    var fi       = Element.Source as FileImageSource;
                    var myFile   = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, fi.File);
                    var myFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(myFile));

                    using (Stream s = await myFolder.OpenStreamForReadAsync(System.IO.Path.GetFileName(myFile)))
                    {
                        var memStream = new MemoryStream();
                        await s.CopyToAsync(memStream);

                        memStream.Position = 0;
                        bitmapImage        = new BitmapImage();
                        bitmapImage.SetSource(memStream.AsRandomAccessStream());
                    }
                }
                else if (file is UriImageSource)
                {
                    bitmapImage = new BitmapImage((Element.Source as UriImageSource).Uri);
                }

                else if (file is StreamImageSource)
                {
                    var handler     = new StreamImageSourceHandler();
                    var imageSource = await handler.LoadImageAsync(file);

                    if (imageSource != null)
                    {
                        Control.Fill = new ImageBrush
                        {
                            ImageSource = imageSource,
                            Stretch     = Stretch.UniformToFill,
                        };
                    }
                    return;
                }

                if (bitmapImage != null)
                {
                    Control.Fill = new ImageBrush
                    {
                        ImageSource = bitmapImage,
                        Stretch     = Stretch.UniformToFill,
                    };
                }
            }
            catch
            {
            }
        }
Example #8
0
        /// <summary>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
            {
                return;
            }

            var min = Math.Min(Element.Width, Element.Height);

            if (min / 2.0f <= 0)
            {
                return;
            }

            try
            {
                Control.Width  = min;
                Control.Height = min;

                // Fill background color
                var color = ((CircleImage)Element).FillColor;
                Control.Fill = new SolidColorBrush(Color.FromArgb(
                                                       (byte)(color.A * 255),
                                                       (byte)(color.R * 255),
                                                       (byte)(color.G * 255),
                                                       (byte)(color.B * 255)));

                // Fill stroke
                color = ((CircleImage)Element).BorderColor;
                Control.StrokeThickness = ((CircleImage)Element).BorderThickness;
                Control.Stroke          = new SolidColorBrush(Color.FromArgb(
                                                                  (byte)(color.A * 255),
                                                                  (byte)(color.R * 255),
                                                                  (byte)(color.G * 255),
                                                                  (byte)(color.B * 255)));

                var force = e.PropertyName == VisualElement.XProperty.PropertyName ||
                            e.PropertyName == VisualElement.YProperty.PropertyName ||
                            e.PropertyName == VisualElement.WidthProperty.PropertyName ||
                            e.PropertyName == VisualElement.HeightProperty.PropertyName ||
                            e.PropertyName == VisualElement.ScaleProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationProperty.PropertyName ||
                            e.PropertyName == CircleImage.BorderThicknessProperty.PropertyName ||
                            e.PropertyName == CircleImage.BorderColorProperty.PropertyName ||
                            e.PropertyName == CircleImage.FillColorProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorXProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorYProperty.PropertyName;

                //already set
                if (file == Element.Source && !force)
                {
                    return;
                }

                file = Element.Source;

                BitmapImage bitmapImage = null;

                // Handle file images
                if (file is FileImageSource)
                {
                    throw new NotImplementedException();
                }

                if (file is UriImageSource)
                {
                    bitmapImage = new BitmapImage((Element.Source as UriImageSource).Uri);
                }
                else if (file is StreamImageSource)
                {
                    var handler     = new StreamImageSourceHandler();
                    var imageSource = await handler.LoadImageAsync(file);

                    if (imageSource != null)
                    {
                        Control.Fill = new ImageBrush
                        {
                            ImageSource = imageSource,
                            Stretch     = Stretch.UniformToFill
                        }
                    }
                    ;
                    return;
                }

                if (bitmapImage != null)
                {
                    Control.Fill = new ImageBrush
                    {
                        ImageSource = bitmapImage,
                        Stretch     = Stretch.UniformToFill
                    }
                }
                ;
            }
            catch
            {
                Debug.WriteLine("Unable to create circle image, falling back to background color.");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected async override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
            {
                return;
            }



            var min = ((CircleImage)Element).CornerRadius;

            if (min / 2.0f <= 0)
            {
                return;
            }

            try
            {
                Control.Width  = min;
                Control.Height = min;

                // Fill background color
                var color = ((CircleImage)Element).FillColor;
                Control.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(
                                                       (byte)(color.A * 255),
                                                       (byte)(color.R * 255),
                                                       (byte)(color.G * 255),
                                                       (byte)(color.B * 255)));

                // Fill stroke
                color = ((CircleImage)Element).BorderColor;
                Control.StrokeThickness = ((CircleImage)Element).BorderThickness;
                Control.Stroke          = new SolidColorBrush(Windows.UI.Color.FromArgb(
                                                                  (byte)(color.A * 255),
                                                                  (byte)(color.R * 255),
                                                                  (byte)(color.G * 255),
                                                                  (byte)(color.B * 255)));

                var force = e.PropertyName == VisualElement.XProperty.PropertyName ||
                            e.PropertyName == VisualElement.YProperty.PropertyName ||
                            e.PropertyName == VisualElement.WidthProperty.PropertyName ||
                            e.PropertyName == VisualElement.HeightProperty.PropertyName ||
                            e.PropertyName == VisualElement.ScaleProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.TranslationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationYProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationXProperty.PropertyName ||
                            e.PropertyName == VisualElement.RotationProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorXProperty.PropertyName ||
                            e.PropertyName == VisualElement.AnchorYProperty.PropertyName;


                //already set
                if (file == Element.Source && !force)
                {
                    return;
                }

                file = Element.Source;

                BitmapImage bitmapImage = null;

                // Handle file images
                if (file is FileImageSource)
                {
                    var fi       = Element.Source as FileImageSource;
                    var myFile   = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, fi.File);
                    var myFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(myFile));

                    using (Stream s = await myFolder.OpenStreamForReadAsync(System.IO.Path.GetFileName(myFile)))
                    {
                        var memStream = new MemoryStream();
                        await s.CopyToAsync(memStream);

                        memStream.Position = 0;
                        bitmapImage        = new BitmapImage();
                        bitmapImage.SetSource(memStream.AsRandomAccessStream());
                    }
                }
                else if (file is UriImageSource)
                {
                    bitmapImage = new BitmapImage((Element.Source as UriImageSource).Uri);
                }
                else if (file is StreamImageSource)
                {
                    var handler     = new StreamImageSourceHandler();
                    var imageSource = await handler.LoadImageAsync(file);

                    if (imageSource != null)
                    {
                        Control.Fill = new ImageBrush
                        {
                            ImageSource = imageSource,
                            Stretch     = Stretch.UniformToFill,
                        };
                    }
                    return;
                }

                if (bitmapImage != null)
                {
                    Control.Fill = new ImageBrush
                    {
                        ImageSource = bitmapImage,
                        Stretch     = Stretch.UniformToFill,
                    };
                }
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Unable to create circle image, falling back to background color.");
            }
        }