public static async Task RunAsync([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string QueueItem, ILogger log) { try { log.LogInformation($"C# Queue trigger function processed"); QueueMessage queueItem = Newtonsoft.Json.JsonConvert.DeserializeObject <QueueMessage>(QueueItem); WeatherData data = new WeatherData(); Weather weather = await data.GetWeatherData(queueItem.cityName); Coordinates coordinates = weather.coord; Stream image; log.LogInformation($"C# Queue trigger function processed: Get beer advice, weather conditions are: {Math.Round(weather.main.Temp - 272.15, 2).ToString()} Celsius"); string beerAdvice = GetBeerAdvice(weather); CloudBlockBlob blob = await GetStorageAccount(log, queueItem); MapData mapdata = new MapData(); image = await mapdata.GetMapData(coordinates.lon, coordinates.lat); string Temp = "Temperature is : " + Math.Round(weather.main.Temp - 272.15, 2).ToString() + " Celcius"; var ImageWithText = ImageHelper.AddTextToImage(image, (Temp, (10, 20)), (beerAdvice, (10, 50))); await blob.UploadFromStreamAsync(ImageWithText); } catch { log.LogError("something went wrong"); } }
private static Bitmap CreateCaptcha(string backgroundImagePath, int captchaLenght, CaptchaCharOptions captchaCharOptions) { string chars; switch (captchaCharOptions) { case CaptchaCharOptions.Alpha: { chars = "ABCDEFGHIJKLMNOPRSTUVYZQWXabcdefghijkmnoprstuvyzqwx"; break; } case CaptchaCharOptions.Numeric: { chars = "0123456789"; break; } case CaptchaCharOptions.Alphanumeric: { chars = "ABCDEFGHIJKLMNOPRSTUVYZQWXabcdefghijkmnoprstuvyzqwx0123456789"; break; } case CaptchaCharOptions.AlphanumericPlus: { chars = "ABCDEFGHIJKLMNOPRSTUVYZQWXabcdefghijkmnoprstuvyzqwx0123456789/*-+()?"; break; } default: { chars = "ABCDEFGHIJKLMNOPRSTUVYZQWXabcdefghijkmnoprstuvyzqwx0123456789"; break; } } var code = ""; var random = new Random(); for (var i = 0; i < captchaLenght; i++) { var character = chars[random.Next(0, chars.Length - 1)]; code += character; } var bitmap = (Bitmap)Image.FromFile(backgroundImagePath); var font = new Font("Times New Roman", 20, FontStyle.Bold); var textColor = Color.Gray; var textBackColor = Color.Transparent; HttpContext.Current.Session[WebCommonConstants.CaptchaKey] = code; return(ImageHelper.AddTextToImage(bitmap, code, 50, DirectionOptions.Center, font, true, textColor, textBackColor, HatchStyle.Percent80)); }
public static async System.Threading.Tasks.Task RunAsync([QueueTrigger("mapqueue", Connection = "AzureWebJobsStorage")] string queueMessage, TraceWriter log) { try { // Create the HttpClient var client = new HttpClient(); // Convert Json message back to its original object (lon, lat, blobname, blobcontainerreference) QueueStorageMessage queueStorageMessage = JsonConvert.DeserializeObject <QueueStorageMessage>(queueMessage); // Retrieve storage account CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage")); // Retrieve container if it exists, create one if not CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference("mapblob"); await container.CreateIfNotExistsAsync(); // Create the blob CloudBlockBlob blockBlob = container.GetBlockBlobReference(queueStorageMessage.blobName); // Retrieve the picture for the given city using its lat and lon var url = String.Format("https://atlas.microsoft.com/map/static/png?subscription-key={0}&api-version=1.0¢er={1},{2}", Environment.GetEnvironmentVariable("MapsAPIKey"), queueStorageMessage.lon, queueStorageMessage.lat); client.BaseAddress = new Uri(url); HttpResponseMessage responseMessage = await client.GetAsync(url); if (responseMessage.IsSuccessStatusCode) { Stream stream = await responseMessage.Content.ReadAsStreamAsync(); // Check if the the temprature is high enough to have a beer string bier = checkForBier(queueStorageMessage); // Format the string to add the celcius degree symbol string temprature = String.Format("Temp: {0} \u2103", queueStorageMessage.temp.ToString()); // Draw on the image using the ImageHelper class Stream renderedImage = ImageHelper.AddTextToImage(stream, (temprature, (10, 20)), (bier, (10, 50))); // Upload the image to the blob await blockBlob.UploadFromStreamAsync(renderedImage); log.Info("City has been found and image was uploaded succesfully"); } else { log.Error("Could not retrieve the map for the given coordinates"); } } catch { log.Error("Something went wrong."); } }
private static async Task <Stream> AddTextToImage(RootWeather weatherRootObject, HttpResponseMessage httpResponseMessage) { int temperature = Convert.ToInt32(weatherRootObject.Main.Temp); double windSpeed = weatherRootObject.Wind.Speed; Stream responseContent = await httpResponseMessage.Content.ReadAsStreamAsync(); string text1 = GetBeerMessage(temperature); string text2 = string.Format($"Temperature: {temperature}"); string text3 = string.Format($"Wind: {windSpeed}km/u"); var renderedImage = ImageHelper.AddTextToImage(responseContent, (text1, (20, 20)), (text2, (20, 50)), (text3, (20, 80))); return(renderedImage); }
public static void Run([QueueTrigger("requestbierrapport", Connection = "")] string myQueueItem, ILogger log) { var item = JsonConvert.DeserializeObject <LocationQueueItem>(myQueueItem); var forecast = OpenWeatherHelper.getForecastFor(item.city, item.country); var image = AzureMapsHelper.getImage(new Tuple <double, double>(forecast.coord.lat, forecast.coord.lon)); string text2 = "Mooi weer voor bier"; if (forecast.main.temp < 18) { text2 = "slecht weer voor bier"; } var editedImage = ImageHelper.AddTextToImage(image, ("het is " + forecast.main.temp.ToString() + " graden buiten", (10, 20)), (text2, (150, 300))); CloudStoarge.upload(item.id + ".png", editedImage as MemoryStream); }