public void RenderToFile(UserControl target, [CallerMemberName] string testName = "") { string path = Path.Combine(testDirectory, testName + ".wpf.out.png"); RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)target.Width, (int)target.Height, 96, 96, PixelFormats.Pbgra32); Size size = new Size(target.Width, target.Height); target.Measure(size); target.Arrange(new Rect(size)); bitmap.Render(target); File.Delete(path); using (FileStream fs = new FileStream(path, FileMode.Create)) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(fs); } }
public static void Initialize(this UIElement element) { var userControl = new UserControl { Content = element }; userControl.BeginInit(); // hacking different things here userControl.Measure(new Size(100, 100)); userControl.Arrange(new Rect(new Size(100, 100))); element.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); }
void Export(UserControl tile, int width, int height, string filename) { tile.Measure(new Size(width, height)); tile.Arrange(new Rect(0, 0, width, height)); var bmp = new WriteableBitmap(width, height); bmp.Render(tile, null); bmp.Invalidate(); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = store.OpenFile(filename, System.IO.FileMode.OpenOrCreate)) { bmp.SaveJpeg(stream, width, height, 0, 100); } }
public void PrintReceipt(UserControl uc) { var dialog = new PrintDialog(); var printer = new PrintServer().GetPrintQueues() .FirstOrDefault(x => x.Name == _printerName); if (printer != null) dialog.PrintQueue = printer; uc.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight)); uc.Arrange(new Rect(new Point(0, 0), uc.DesiredSize)); dialog.PrintVisual(uc, "receipt"); }