protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
      {
        base.OnElementChanged(e);

        if (_formsControl != null)
        {
          var svgStream = _formsControl.SvgAssembly.GetManifestResourceStream(_formsControl.SvgPath);

          if (svgStream == null)
          {
            throw new Exception(string.Format("Error retrieving {0} make sure Build Action is Embedded Resource",
              _formsControl.SvgPath));
          }

          var r = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser()), new ValuesParser());

          var graphics = r.Graphic;

          var width = _formsControl.WidthRequest <= 0 ? 100 : _formsControl.WidthRequest;
          var height = _formsControl.HeightRequest <= 0 ? 100 : _formsControl.HeightRequest;

          var scale = 1.0;

          if (height >= width)
          {
            scale = height/graphics.Size.Height;
          }
          else
          {
            scale = width/graphics.Size.Width;
          }

          var scaleFactor = UIScreen.MainScreen.Scale;

          var canvas = new ApplePlatform().CreateImageCanvas(graphics.Size, scale*scaleFactor);
          graphics.Draw(canvas);
          var image = canvas.GetImage();

          var uiImage = image.GetUIImage();
          Control.Image = uiImage;
        }
      }
Exemplo n.º 2
0
 public TextMetrics MeasureText(string text, Font font)
 {
     return(ApplePlatform.GlobalMeasureText(text, font));
 }
Exemplo n.º 3
0
        private async void GenerateImage(object sender, EventArgs e)
        {
            _avatar.AvatarInitial = "C";
            _avatar.FillColor = Color.Blue;

            /*
             * 
                    01 - #71cfbf
                    02 - #e3a685
                    03 - #7eaae5
                    04 - #ca7c7a
                    05 - #bc789b
                    06 - #dd8785
             */
            var colours = new List<string>()
                          {
                              "#71cfbf","#e3a685", "#7eaae5", "#ca7c7a", "#bc789b", "#dd8785"
                          };

            // (NGraphics.IImageCanvas)
            double canvasSize = 96;
            double textSize = canvasSize / 2;

            var canvasx = new ApplePlatform().CreateImageCanvas(new Size(canvasSize), 2);
            //canvasx.FillEllipse(rect, new RadialGradientBrush(new NGraphics.Color("#0000ff"), new NGraphics.Color("#ff0000")));

            //canvasx.DrawRectangle(rect, new Pen("#9966CC", 5D), new SolidBrush(NGraphics.Color.FromRGB(0)));
            var rect = new Rect(new Size(canvasSize));

            var initials = new string[]
                            {
                                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                                "S", "T", "U", "V", "W", "X", "Y", "Z"
                            };

            foreach(var colourString in colours)
            {
                var backColour = new NGraphics.Color(colourString);
                canvasx.DrawRectangle(rect, new Pen(backColour), new SolidBrush(backColour));

                double penWidth = 1D;

                var innerBox = new Rect(new Point(rect.Center.X - textSize / 2, rect.Center.Y - textSize / 2), new Size(textSize));
                // DEBUG only to see where the text appears within the parent container for centering purposes
                //canvasx.DrawRectangle(innerBox, new Pen(Colors.LightGray));

                // bug in NGraphics on ios draws text ABOVE the rectangle you specify
                var textRec = new Rect(new Point(innerBox.Left + penWidth, innerBox.Top + innerBox.Height - penWidth - (textSize * 0.1)), new Size(textSize));
                var font = new Font("Metric-Regular", textSize);

                canvasx.DrawText("W", textRec, font, TextAlignment.Center, Pens.White, Brushes.White);
                //canvasx.DrawText("W", rect.Center, new Font(), new NGraphics.Color("#9966cc") );

                string fileName = System.IO.Path.Combine(_dataPath, string.Format("{0:dd-MM-yyyy-HH-mm-ss}.png", DateTime.Now));
                canvasx.GetImage().SaveAsPng(fileName);
                LogHost.Default.Debug(string.Format("image saved to {0}", fileName));

                _drawnImage.Source = ImageSource.FromFile(fileName);
                // var s = colourString;
                // await MainPage.DisplayAlert("Done", string.Format("Done with {0}", s), "OK");
            }
            Device.BeginInvokeOnMainThread(async () => await this.DisplayAlert("All Done", "All Done", "OK"));
            
        }