× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Copy UI element with adorner

Gayot Fow
Feb 05, 2015
<p>You can use the native WPF Printing name space to print to an XPS file and this will include the adorner in the result (I tested it successfully)...</p> <pre><code>using System.Windows.Controls; private void ExecutePrintCommand(object obj) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { printDialog.PrintVisual(_mainWindow, "Main Window with Adorner"); } } </code></pre> <p>If you did not want to use the PrintDialog (which actually opens a dialog box). You can use the XpsDocumentWriter class to programmatically control the process. The enabling snippet for this is...</p> <pre><code> XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(q); xpsdw.Write(viewer.Document); </code></pre> <p>...which was extracted from here: <a href="http://stackoverflow.com/questions/601496/print-fixeddocument-programmatically">Print FixedDocument programmatically</a> And there are more articles about fine-tuning the process if that is part of your requirements. NOTE that the XPS file is actually a 'zip' file masquerading as an 'xps' file, so you can unzip it by changing the extension to see if the contents are of any use.</p> <p>Secondarily, I tested saving a window with an adorner on a TextBox with this code...</p> <pre><code> private void SaveWithAdorner() { RenderTargetBitmap rtb = RenderVisaulToBitmap(_mainWindow, 500, 300); MemoryStream file = new MemoryStream(); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(file); using (FileStream fstream = File.OpenWrite("Myimage.jpg")) { file.WriteTo(fstream); fstream.Flush(); fstream.Close(); } } </code></pre> <p>...with good results. I.e., the adorner appeared in the saved bitmap with its red border. This might differ from your code because I use a Png encoder (but saved to a 'jpg' file). </p> <p>Although I have tested both approaches successfully, you'll need to check them on your hardware.</p> <p>And lastly, as a last-ditch resort, you can deactivate WPF's hardware rendering mode and set it to software rendering...</p> <pre><code>RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; </code></pre> <p>...for which there's a nice SO thread here: <a href="http://stackoverflow.com/questions/4951058/software-rendering-mode-wpf">Software rendering mode - WPF</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/17969651/Copy%20UI%20element%20with%20adorner/18304452">Stack Overflow</a>.</p>
comments powered by Disqus