private async void DrawKey(CovidWorldwideStats stats) { const int ICON_STARTING_X = 3; const int ICON_PADDING_Y = 3; const int TEXT_PADDING_Y = 3; const int TEXT_PADDING_X = 40; const int ICON_SIZE_PIXELS = 35; if (stats == null) { return; } if (!long.TryParse(stats.Recovered, out long recovered) || !long.TryParse(stats.Deaths, out long deaths) || !long.TryParse(stats.AllCases, out long allCases)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"CoronavirusWorldwideStatsAction > Could not convert deaths/all cases to integer Deaths: {stats.Deaths} All Case: {stats.AllCases}"); return; } // Get the recovery rate as a percentage double recoveryRate = (double)recovered / (double)allCases * 100; using (Bitmap img = Tools.GenerateGenericKeyImage(out Graphics graphics)) { int height = img.Height; int width = img.Width; float heightPosition = 10; string text; var font = new Font("Verdana", 22, FontStyle.Bold, GraphicsUnit.Pixel); var fontRecoveryTitle = new Font("Verdana", 20, FontStyle.Bold, GraphicsUnit.Pixel); var fontRecovery = new Font("Verdana", 30, FontStyle.Bold, GraphicsUnit.Pixel); Bitmap icon; using (icon = IconChar.Ambulance.ToBitmap(ICON_SIZE_PIXELS, Color.Orange)) { graphics.DrawImage(icon, new Point(ICON_STARTING_X, (int)heightPosition)); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, GraphicUtils.FormatNumber(allCases), font, Brushes.Orange, new PointF(TEXT_PADDING_X, heightPosition + +TEXT_PADDING_Y)); } heightPosition += ICON_PADDING_Y; using (icon = IconChar.SkullCrossbones.ToBitmap(ICON_SIZE_PIXELS, Color.Red)) { graphics.DrawImage(icon, new Point(ICON_STARTING_X, (int)heightPosition)); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, GraphicUtils.FormatNumber(deaths), font, Brushes.Red, new PointF(TEXT_PADDING_X, heightPosition + TEXT_PADDING_Y)); } heightPosition += ICON_PADDING_Y; heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, "Recovered:", fontRecoveryTitle, Brushes.Green, new PointF(ICON_STARTING_X, heightPosition)); // Put percentage exactly in middle text = $"{(int)recoveryRate}%"; float stringWidth = GraphicUtils.CenterText(text, width, fontRecovery, graphics, ICON_STARTING_X); GraphicUtils.DrawStringOnGraphics(graphics, text, fontRecovery, Brushes.Green, new PointF(stringWidth, heightPosition)); await Connection.SetImageAsync(img); graphics.Dispose(); } }
private async void DrawKey(CovidCountryStats stats) { const int ICON_STARTING_X = 3; const int TEXT_PADDING_Y = 3; const int TEXT_PADDING_X = 40; const int ICON_SIZE_PIXELS = 35; const int COUNTRY_NAME_PADDING_Y = 10; if (stats == null) { return; } if ((DateTime.Now - lastStageChange).TotalSeconds >= STAGE_CHANGE_SECONDS) { lastStageChange = DateTime.Now; currentStage = (currentStage + 1) % TOTAL_STAGES; } if (!long.TryParse(stats.Recovered, out long recovered) || !long.TryParse(stats.Deaths, out long deaths) || !long.TryParse(stats.Cases, out long allCases)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"CoronavirusCountryStatsAction > Could not convert deaths/all cases to integer Deaths: {stats.Deaths} Case: {stats.Cases}"); return; } // Get the recovery rate as a percentage double recoveryRate = (double)recovered / (double)allCases * 100; using (Bitmap img = Tools.GenerateGenericKeyImage(out Graphics graphics)) { graphics.PageUnit = GraphicsUnit.Pixel; int height = img.Height; int width = img.Width; float heightPosition = 10; string text; if (settings.ShowFlag && stats.Info != null && !String.IsNullOrEmpty(stats.Info.FlagURL)) { using (Bitmap flag = FetchImage(stats.Info.FlagURL)) { if (flag != null) { float opacity = flagOpacity / 100f; using (Image opacityFlag = GraphicUtils.SetImageOpacity(flag, opacity)) { graphics.DrawImage(opacityFlag, 0, 0, img.Width, img.Height); } } } } var font = new Font("Verdana", 23, FontStyle.Bold, GraphicsUnit.Pixel); var fontRecoveryTitle = new Font("Verdana", 20, FontStyle.Bold, GraphicsUnit.Pixel); var fontRecovery = new Font("Verdana", 30, FontStyle.Bold, GraphicsUnit.Pixel); Bitmap icon; float stringWidth = GraphicUtils.CenterText(stats.Name, width, font, graphics, 0); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, stats.Name, font, Brushes.White, new PointF(stringWidth, heightPosition)); heightPosition += COUNTRY_NAME_PADDING_Y; float widthPosition = 0; switch (currentStage) { case 0: // All Cases /* * using (icon = IconChar.Ambulance.ToBitmap(height, Color.Gray)) * { * text = $"{Utils.FormatNumber(allCases)}\n({stats.TodayCases})"; * widthPosition = Utils.CenterText(text, width, font, graphics); * using (Image opacityIcon = Utils.SetImageOpacity(icon, 0.5f)) * { * graphics.DrawImage(opacityIcon, new PointF(0, COUNTRY_NAME_PADDING_Y)); * } * heightPosition = Utils.DrawStringOnGraphics(graphics, text, font, Brushes.Orange, new PointF(widthPosition, heightPosition + TEXT_PADDING_Y)); * } */ using (icon = IconChar.Ambulance.ToBitmap(ICON_SIZE_PIXELS, Color.Orange)) { text = $"{GraphicUtils.FormatNumber(allCases)}"; widthPosition = GraphicUtils.CenterText(text, width, font, graphics); widthPosition -= ((ICON_SIZE_PIXELS + ICON_STARTING_X) / 2); // Add room for the icon graphics.DrawImage(icon, new PointF(widthPosition, (int)heightPosition)); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, text, font, Brushes.Orange, new PointF(widthPosition + ICON_SIZE_PIXELS + ICON_STARTING_X, heightPosition + TEXT_PADDING_Y)); text = $"({stats.TodayCases})"; widthPosition = GraphicUtils.CenterText(text, width, font, graphics); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, text, font, Brushes.Orange, new PointF(widthPosition, heightPosition)); } break; case 1: // Deaths using (icon = IconChar.SkullCrossbones.ToBitmap(ICON_SIZE_PIXELS, Color.Red)) { text = $"{GraphicUtils.FormatNumber(deaths)}"; widthPosition = GraphicUtils.CenterText(text, width, font, graphics); widthPosition -= ((ICON_SIZE_PIXELS + ICON_STARTING_X) / 2); // Add room for the icon graphics.DrawImage(icon, new PointF(widthPosition, (int)heightPosition)); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, text, font, Brushes.Red, new PointF(widthPosition + ICON_SIZE_PIXELS + ICON_STARTING_X, heightPosition + TEXT_PADDING_Y)); text = $"({ stats.TodayDeaths})"; widthPosition = GraphicUtils.CenterText(text, width, font, graphics); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, text, font, Brushes.Red, new PointF(widthPosition, heightPosition)); } break; case 2: // Recovery text = "Recovered:"; widthPosition = GraphicUtils.CenterText(text, width, font, graphics, 3); heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, text, fontRecoveryTitle, Brushes.Green, new PointF(widthPosition, heightPosition)); // Put percentage exactly in middle text = $"{(int)recoveryRate}%"; widthPosition = GraphicUtils.CenterText(text, width, fontRecovery, graphics, ICON_STARTING_X); GraphicUtils.DrawStringOnGraphics(graphics, text, fontRecovery, Brushes.Green, new PointF(widthPosition, heightPosition)); break; } await Connection.SetImageAsync(img); graphics.Dispose(); } }