Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Instantiate User control programmatically to render it as PNG
    primarykey
    data
    text
    <p>I want to instantiate a user control programmatically in a DLL to save it afterwards as PNG file. This is generally no problem with PngBitmapEncoder and RenderTargetBitmap.</p> <p>This are my questions:</p> <ul> <li>How do I instantiate the control? Simply with the new-operator?</li> <li>Do I have to instantiate it in an seperate thread?</li> <li>How do I force the control to update all its children and to render itself again?</li> </ul> <p>This is my code to instantiate the user control and save it as PNG-file (LetterFrequency is the user control):</p> <pre><code> [STAThread] static void Main(string[] args) { LetterFrequency let = new LetterFrequency(); let.Width = 600; let.Height = 400; let.Background = Brushes.White; let.Measure(new Size(let.Width, let.Height)); let.Arrange(new Rect(new Size(let.Width, let.Height))); let.UpdateLayout(); RenderTargetBitmap bitmap = new RenderTargetBitmap((int)let.Width, (int)let.Height, 96d, 96d, PixelFormats.Pbgra32); bitmap.Render(let); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bitmap)); using (Stream stm = File.Create("test.png")) { png.Save(stm); } } </code></pre> <p>If you run the app this way, it generates the PNG file, but the data, which will be added in the XAML are not visible, if you look into the XAML Designer, you can see the chart with some bubbles. The png file contains only the chart area, but no bubbles? Why that? I think is is a Update/rendering problem, but how to solve this?</p> <p>Here is the visual studio solution, it contains the Console Project, which renders the user control to a PNG file and two other projects of the WPF toolkit for the chart.</p> <p>Have a look at it, it will generate the PNG file in the bin/Debug respectively in the exe-folder: <a href="http://www.file-upload.net/download-1904406/ChartRenderBitmap.zip.html" rel="noreferrer">http://www.file-upload.net/download-1904406/ChartRenderBitmap.zip.html</a></p> <p>Hope it works without problems!</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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