예제 #1
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"));
            
        }