Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate Image at Runtime
    primarykey
    data
    text
    <p>I am trying to render some XAML at runtime. My XAML is dynamically constructed in a StringBuilder. As an example, here is a sample:</p> <pre><code>StringBuilder xaml = new StringBuilder(); xaml.Append("&lt;Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" "); xaml.Append("xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Height=\"135\" "); xaml.Append("Width=\"210\" Background=\"White\" x:Name=\"Page\"&gt;"); string url = "http://www.mydomain.com/images/background.png"; xaml.Append("&lt;Image Width=\"300\" Height=\"200\" Canvas.Left=\"0\" Canvas.Top=\"0\" "); xaml.Append("x:Name=\"bgImage\" Source=\""); xaml.Append(url); xaml.Append("\" /&gt;"); xaml.Append("&lt;/Canvas&gt;"); </code></pre> <p>Please notice the Image element in this sample. If I put this XAML in a .xaml file it runs fine. If I paste it into Blend, it works fine. However, I need to wait for this image to be downloaded and then convert the rendered XAML to a .png. In order to do this, I am using the following code:</p> <pre><code>StringReader stringReader = new StringReader(xaml.ToString()); XmlTextReader xmlReader = new XmlTextReader(stringReader); FrameworkElement frameworkElement = (FrameworkElement)(XamlReader.Load(xmlReader)); Size availableSize = new Size(300, 200); frameworkElement.Measure(availableSize); frameworkElement.Arrange(new Rect(availableSize)); BitmapSource bitmap = RenderToBitmap(frameworkElement); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); FileStream fileStream = new FileStream(filename, FileMode.Create); encoder.Save(fileStream); fileStream.Close(); </code></pre> <p>The problem is, the .png that is generated always ignores the Image element. I am guessing because the Image source is not downloaded. How do I generate an image from some XAML at runtime that includes an Image?</p> <p>Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload