public static async Task<Bitmap> GetBitmapAsync (SvgImage svgImage, int width, int height) { Bitmap result = null; Stream svgStream = await SvgService.GetSvgStreamAsync (svgImage).ConfigureAwait (false); await Task.Run(() => { var svgReader = new SvgReader(new StreamReader(svgStream)); var graphics = svgReader.Graphic; var scale = 1.0; if (height >= width) { scale = height / graphics.Size.Height; } else { scale = width / graphics.Size.Width; } var canvas = new AndroidPlatform().CreateImageCanvas(graphics.Size, scale); graphics.Draw(canvas); var image = (BitmapImage)canvas.GetImage(); result = image.Bitmap; }); return result; }
public static async Task<Stream> GetSvgStreamAsync (SvgImage svgImage) { Stream stream = null; //Insert into Dictionary if (!SvgStreamByPath.ContainsKey (svgImage.SvgPath)) { if (svgImage.SvgAssembly == null) throw new Exception ("Svg Assembly not specified. Please specify assembly using the SvgImage Control SvgAssembly property."); stream = svgImage.SvgAssembly.GetManifestResourceStream (svgImage.SvgPath); if (stream == null) throw new Exception (string.Format ("Not able to retrieve svg from {0} make sure the svg is an Embedded Resource and the path is set up correctly", svgImage.SvgPath)); SvgStreamByPath.Add (svgImage.SvgPath, stream); return await Task.FromResult<Stream> (stream); } //Get from dictionary stream = SvgStreamByPath [svgImage.SvgPath]; //Reset the stream position otherwise an error is thrown (SvgFactory.GetBitmap sets the position to position max) stream.Position = 0; return await Task.FromResult<Stream> (stream); }
public ContentView CreateImageButton (string buttonImage, double height, double width, Action tappedCallback) { var grid = new Grid { RowDefinitions = new RowDefinitionCollection { new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }, }, ColumnDefinitions = new ColumnDefinitionCollection { new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) }, }, BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.Center, RowSpacing = 5 }; var navImage = new SvgImage { SvgPath = string.Format ("ExtendedMap.Forms.Plugin.Abstractions.Images.{0}", buttonImage), SvgAssembly = typeof(CustomMapContentView).GetTypeInfo ().Assembly, HorizontalOptions = LayoutOptions.Center, HeightRequest = height, WidthRequest = width }; grid.GestureRecognizers.Add (new TapGestureRecognizer{ Command = new Command (tappedCallback) }); grid.Children.Add (navImage, 0, 0); return new ContentView { Content = grid }; }
private static void OnReplacementColorsPropertyChanged(BindableObject d, ColorPairs in_OldValue, ColorPairs in_NewValue) { SvgImage element = d as SvgImage; if (element == null) { return; } if (in_OldValue != null) { in_OldValue.CollectionChanged -= element.ColorPairs_CollectionChanged; } if (in_NewValue != null) { in_NewValue.CollectionChanged -= element.ColorPairs_CollectionChanged; in_NewValue.CollectionChanged += element.ColorPairs_CollectionChanged; } if (in_NewValue == in_OldValue) { return; } element.OnReplacementColorsChanged( ); }
public ViaCode() { Title = "Via Code"; //Get SVGs from http://thenounproject.com/ var svgPaths = new List<string> { "SampleApp.Images.CoolMask.svg", "SampleApp.Images.Elvis.svg", "SampleApp.Images.tiger.svg", //TEMP REMOVING THE SPACE SHIPS UNTIL SUPPORT FOR svg is better for Win Phone //"SampleApp.Images.ErulisseuiinSpaceshipPack.svg", }; var grid = new Grid { RowDefinitions = new RowDefinitionCollection(), ColumnDefinitions = new ColumnDefinitionCollection() }; for (var rowIndex = 0; rowIndex < svgPaths.Count; rowIndex++) { var svgPath = svgPaths[rowIndex]; grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); for (var columnIndex = 0; columnIndex <= 4; columnIndex++) { grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); //IMPORTANT Make sure you set both SvgPath and SvgAssembly var svgImage = new SvgImage { SvgPath = svgPath, SvgAssembly = typeof(App).GetTypeInfo().Assembly, HeightRequest = columnIndex * 50, WidthRequest = columnIndex * 50, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, BackgroundColor = Color.White }; grid.Children.Add(svgImage, columnIndex, rowIndex); } } var verticalScrollView = new ScrollView { Orientation = ScrollOrientation.Vertical, Content = grid }; var horizontalScrollView = new ScrollView { Orientation = ScrollOrientation.Horizontal, Content = verticalScrollView, }; Content = horizontalScrollView; }
public SVGPage() { //Get SVGs from http://thenounproject.com/ var svgPaths = new List<string> { "PluginSampleApp.Images.CoolGuy.svg", "PluginSampleApp.Images.Elvis.svg", "PluginSampleApp.Images.tiger.svg", }; var grid = new Grid { RowDefinitions = new RowDefinitionCollection(), ColumnDefinitions = new ColumnDefinitionCollection() }; var svgId = 0; for (var rowIndex = 0; rowIndex < svgPaths.Count; rowIndex++) { var svgPath = svgPaths[rowIndex]; grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto}); for (var columnIndex = 0; columnIndex <= 4; columnIndex++,svgId++) { grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto}); //IMPORTANT Make sure you set both SvgPath and SvgAssembly var svgImage = new SvgImage { SvgPath = svgPath, StyleId = string.Format("svg" + svgId), SvgAssembly = typeof (App).GetTypeInfo().Assembly, HeightRequest = columnIndex*50, WidthRequest = columnIndex*50, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, BackgroundColor = Color.White }; grid.Children.Add(svgImage, columnIndex, rowIndex); } } var verticalScrollView = new ScrollView {Orientation = ScrollOrientation.Vertical, Content = grid}; var horizontalScrollView = new ScrollView { Orientation = ScrollOrientation.Horizontal, Content = verticalScrollView, }; Content = horizontalScrollView; }
public ContentView CreateImageButton (string buttonImage, string buttonText, double height, double width, Action tappedCallback) { var relativeLayout = new RelativeLayout {BackgroundColor = Color.Transparent}; relativeLayout.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(tappedCallback) }); var svgImage = new SvgImage { SvgPath = string.Format ("ExtendedMap.Forms.Plugin.Abstractions.Images.{0}", buttonImage), SvgAssembly = typeof(CustomMapContentView).GetTypeInfo ().Assembly, HeightRequest = height, WidthRequest = width }; var svgImageX = Device.OnPlatform (0.3,0.36,0.36); var svgImageWidth = Device.OnPlatform (0.45,0.3,0.45); relativeLayout.Children.Add (svgImage, Constraint.RelativeToParent ((parent) => (parent.Width * svgImageX)), Constraint.RelativeToParent ((parent) => (parent.Height * 0)), Constraint.RelativeToParent ((parent) => (parent.Width * svgImageWidth)), Constraint.RelativeToParent ((parent) => (parent.Height * 0.45)) ); var label = new Label { Text = buttonText, Font = Font.SystemFontOfSize (14), TextColor = Colors.DarkBlue, HorizontalOptions = LayoutOptions.Center }; var stackLayout = new StackLayout (); stackLayout.Children.Add (label); relativeLayout.Children.Add (stackLayout, Constraint.RelativeToParent ((parent) => (parent.Width * 0)), Constraint.RelativeToParent ((parent) => (parent.Height * 0.6)), Constraint.RelativeToParent ((parent) => (parent.Width * 1)), Constraint.RelativeToParent ((parent) => (parent.Height * 0.4)) ); return new ContentView { Content = relativeLayout }; }
public void Initialize() { Image = new SvgImage { WidthRequest = 34, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center, SvgAssembly = typeof(App).GetTypeInfo ().Assembly, SvgPath = "ClothesAndWeather.Images." + Type.ToString ().ToLower () + "-" + (Direction == -1 ? "left" : "right") + ".svg" }; this.Content = Image; Opacity = 0; double y = 0; double h = 0; switch (Type) { case ClothType.Head: y = 0; h = 0.18; break; case ClothType.Body: y = 0.25; h = 0.3; break; case ClothType.Pants: y = 0.71; h = 0.32; break; case ClothType.Shoes: y = 1; h = 0.2; break; } AbsoluteLayout.SetLayoutBounds (this, new Rectangle (Direction == -1 ? 0 : 1, y, 0.2, h)); AbsoluteLayout.SetLayoutFlags (this, AbsoluteLayoutFlags.All); }
public App() { _ViewModel = new TestModel(); var insetLabel = new Label(); insetLabel.SetBinding(Label.TextProperty, nameof(TestModel.SvgInsets), stringFormat: "Stretchable Insets: {0:C2}"); var resourcePicker = new Picker() { HorizontalOptions = LayoutOptions.FillAndExpand, }; foreach (var resourceName in TestModel.AvailableResourceNames) { resourcePicker.Items.Add(resourceName); } resourcePicker.SetBinding(Picker.SelectedIndexProperty, nameof(TestModel.SvgResourceIndex), BindingMode.TwoWay); var insetSlider = new Slider() { Minimum = 0, Maximum = 35, Value = _ViewModel.AllSidesInset, }; insetSlider.SetBinding(Slider.ValueProperty, nameof(TestModel.AllSidesInset), BindingMode.TwoWay); var slicingSvg = new SvgImage() { SvgAssembly = typeof(App).GetTypeInfo().Assembly, WidthRequest = 300, HeightRequest = 300, }; slicingSvg.SetBinding(SvgImage.SvgStretchableInsetsProperty, nameof(TestModel.SvgInsets)); slicingSvg.SetBinding(SvgImage.SvgPathProperty, nameof(TestModel.SvgResourcePath)); var svgButton = new Button() { WidthRequest = 300, HeightRequest = 300, }; // The root page of your application MainPage = new NavigationPage (new ContentPage { Title = "9-Slice SVG Scaling", Content = new StackLayout { VerticalOptions = LayoutOptions.Start, HorizontalOptions = LayoutOptions.Center, Children = { insetLabel, resourcePicker, insetSlider, new AbsoluteLayout() { WidthRequest = 300, HeightRequest = 300, Children = { slicingSvg, svgButton, }, }, }, BindingContext = _ViewModel, }, }); svgButton.Clicked += (sender, e) => { MainPage.DisplayAlert("Tapped!", "SVG button tapped!", "OK"); }; }