public async Task <ActionResult> Index(string cardUrl = null) { cardUrl = cardUrl ?? "https://raw.githubusercontent.com/Microsoft/AdaptiveCards/master/samples/v1.0/Scenarios/ActivityUpdate.json"; try { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); // Get the JSON from the card URL var client = new HttpClient(); var response = await client.GetAsync(cardUrl, cts.Token); var json = await response.Content.ReadAsStringAsync(); // Make sure the payload has a version property var jObject = JObject.Parse(json); if (!jObject.TryGetValue("version", out var _)) { jObject["version"] = "0.5"; } // Parse the Adaptive Card JSON AdaptiveCardParseResult parseResult = AdaptiveCard.FromJson(jObject.ToString()); AdaptiveCard card = parseResult.Card; // Create a host config AdaptiveHostConfig hostConfig = new AdaptiveHostConfig() { SupportsInteractivity = false }; // TODO: Load custom host configs // Create a renderer AdaptiveCardRenderer renderer = new AdaptiveCardRenderer(hostConfig); // Set any XAML resource Dictionary if you have one renderer.ResourcesPath = System.IO.Path.Combine(_env.WebRootPath, "Styles", "MyStyles.xaml"); // Render the card to bitmap RenderedAdaptiveCardImage renderedCard = await renderer.RenderCardToImageAsync(card, createStaThread : true, cancellationToken : cts.Token); return(File(renderedCard.ImageStream, "image/png")); } catch (OperationCanceledException) { // Timed out return(StatusCode((int)HttpStatusCode.RequestTimeout)); } catch (Exception) { // TODO: log throw; } }
private async static Task RenderAdaptiveCard(Attachment attachment) { // This is entirely optional, just shows how to render an adaptive card which in a console app isn't really very practical! try { AdaptiveCardParseResult result = AdaptiveCard.FromJson(attachment.Content.ToString()); var adaptiveCard = AdaptiveCard.FromJson(attachment.Content.ToString()); if (adaptiveCard != null) { // Create a host config with no interactivity // (buttons in images would be deceiving) AdaptiveHostConfig hostConfig = new AdaptiveHostConfig() { SupportsInteractivity = false }; // Create a renderer AdaptiveCardRenderer renderer = new AdaptiveCardRenderer(hostConfig); // Render the card to png RenderedAdaptiveCardImage renderedCard = await renderer.RenderCardToImageAsync(adaptiveCard.Card, createStaThread : true, cancellationToken : default(CancellationToken)); string fileName = $"{Guid.NewGuid()}.png"; using (var fileStream = File.Create(fileName)) { renderedCard.ImageStream.Seek(0, SeekOrigin.Begin); renderedCard.ImageStream.CopyTo(fileStream); } Console.WriteLine($"Adaptive Card rendered to {fileName} for debug purposes."); Process.Start(fileName); } Console.WriteLine($"Adaptive Card Speak Property: {adaptiveCard.Card.Speak}"); } catch (Exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Adaptive card not parsed"); Console.ForegroundColor = ConsoleColor.Gray; } }
private static async Task RenderCard(string file, AdaptiveCardRenderer renderer, string outPath) { try { var watch = new Stopwatch(); watch.Start(); AdaptiveCardParseResult parseResult = AdaptiveCard.FromJson(File.ReadAllText(file, Encoding.UTF8)); AdaptiveCard card = parseResult.Card; // Timeout after 30 seconds var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); // Render the card to an image RenderedAdaptiveCardImage renderedCard = await renderer.RenderCardToImageAsync(card, true, 400, cts.Token); // Report any warnings foreach (var warning in parseResult.Warnings.Union(renderedCard.Warnings)) { Console.WriteLine($"[{Path.GetFileName(file)}] WARNING: {warning.Message}"); } // Write to a png file with the same name as the json file var outputFile = Path.Combine(outPath, Path.ChangeExtension(Path.GetFileName(file), ".png")); using (var fileStream = new FileStream(outputFile, FileMode.Create)) { renderedCard.ImageStream.CopyTo(fileStream); Console.WriteLine($"[{watch.ElapsedMilliseconds}ms T{Thread.CurrentThread.ManagedThreadId}]\t{Path.GetFileName(file)} => {Path.GetFileName(outputFile)}"); } } catch (Exception ex) { Console.Error.WriteLine($"[FAILED]\t{Path.GetFileName(file)} => {ex.Message}"); } }