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 BitmapImage GetImage(UserControl pageControl, out int imageWidth, out int imageHeight) { pageControl.Width = 520; pageControl.Height = 400; imageWidth = (int)pageControl.Width; imageHeight = (int)pageControl.Height; pageControl.Measure(new Size(width: double.PositiveInfinity, height: double.PositiveInfinity)); var sz = pageControl.DesiredSize; var rect = new Rect(sz); pageControl.Arrange(rect); var bmp = new RenderTargetBitmap((int)(rect.Width), (int)(rect.Height), 96, 96, PixelFormats.Default); bmp.Render(pageControl); var bitmapImage = new BitmapImage(); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(bmp)); using (var stream = new MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } return(bitmapImage); }
public static void SaveTo(this UserControl control, string filename, Size size) { var controlWidth = (int)size.Width; var controlHeight = (int)size.Height; // Set size of the control control.Height = controlHeight; control.Width = controlWidth; control.Arrange(new Rect(0, 0, controlWidth, controlHeight)); // Create writeable bitmap to save as image var bmp = new RenderTargetBitmap(controlWidth, controlHeight, 120, 120, PixelFormats.Pbgra32); bmp.Render(control); var png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bmp)); var stream = new MemoryStream(); png.Save(stream); var fileStream = File.Create(Directory.GetCurrentDirectory() + string.Format(@"\{0}.png", filename)); stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); fileStream.Close(); }
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)); }
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"); }
public static void RenderTile(int width, int height, string number, object context) { string path = string.Format("/Shared/ShellContent/{0}_{1}_{2}.jpg", width, height, number); WriteableBitmap bmp = null; UserControl control = null; ManualResetEventSlim evt = new ManualResetEventSlim(false, 50); var act = new Action(() => { evt.Reset(); control = (width == 0x9f) ? _159_159 as UserControl : _336_336 as UserControl; control.DataContext = context; control.Measure(new Size((double)width, (double)height)); control.UpdateLayout(); control.Arrange(new Rect(0.0, 0.0, (double)width, (double)height)); control.UpdateLayout(); bmp = new WriteableBitmap(width, height); bmp.Render(control, null); bmp.Invalidate(); evt.Set(); }); if (!Deployment.Current.Dispatcher.CheckAccess()) { Deployment.Current.Dispatcher.BeginInvoke(act); } else { act(); } evt.Wait(); if (bmp != null) { using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, FileMode.Create, FileAccess.Write, file)) { PNGWriter.WritePNG(bmp, stream, -1); //Extensions.SaveJpeg(bmp, stream, width, height, 0, 100); } } } bmp = null; }
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); } }
private void RenderImageWorker() { IRenderTemplate renderTemplate = new GalleryCoverRenderTemplate(); Random r = new Random(); double imageRotation = r.Next(_imageRotationMin, _imageRotationMax); double titleRotation = r.Next(_titleRotationMin, _titleRotationMax); double titleOffsetX = r.Next(_titleOffestXMin, _titleOffestXMax); double titleOffsetY = r.Next(_titleOffestYMin, _titleOffestYMax); double imageOffsetX = r.Next(_imageOffestXMin, _imageOffestXMax); double imageOffsetY = r.Next(_imageOffestYMin, _imageOffestYMax); renderTemplate.ApplyImage(_coverImagePath); renderTemplate.ApplyTitle(_title); renderTemplate.FormatTitle(titleRotation, titleOffsetX, titleOffsetY); renderTemplate.FormatImage(imageRotation, imageOffsetX, imageOffsetY); renderTemplate.ResolveFonts(_contentPath); UserControl renderControl = (UserControl)renderTemplate; // Update layout after all the details have been applied. renderControl.Measure(new Size(renderControl.Width, renderControl.Height)); renderControl.Arrange(new Rect(new Size(renderControl.Width, renderControl.Height))); // Render the image to a bitmap. RenderTargetBitmap bitmapRenderer = new RenderTargetBitmap((int)renderControl.ActualWidth, (int)renderControl.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmapRenderer.Render(renderControl); // Encode the bitmap as png. PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bitmapRenderer)); // Output the png to the file. using (MemoryStream memoryStream = new MemoryStream()) { png.Save(memoryStream); this.imageBuffer = memoryStream.ToArray(); } if (bitmapRenderer.Dispatcher.Thread.IsAlive) { bitmapRenderer.Dispatcher.InvokeShutdown(); } }
//public static System.Windows.Forms.Cursor Convert(System.Windows.Input.Cursor cursor) //{ // return new System.Windows.Forms.Cursor(cursor.) //} public static RenderTargetBitmap CreateBitmap(UserControl control, Size desiredSize) { control.Width = desiredSize.Width; control.Height = desiredSize.Height; control.Measure(desiredSize); control.Arrange(new Rect(desiredSize)); control.UpdateLayout(); RenderTargetBitmap bmp = new RenderTargetBitmap( (int)desiredSize.Width, (int)desiredSize.Height, 96, 96, PixelFormats.Pbgra32); bmp.Render(control); return(bmp); }
private static RenderTargetBitmap RenderScreenAndSave(UserControl view) { System.Windows.Size size = new System.Windows.Size(view.RenderSize.Width, view.RenderSize.Height); RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, System.Windows.Media.PixelFormats.Pbgra32); System.Windows.Shapes.Rectangle vRect = new System.Windows.Shapes.Rectangle() { Width = size.Width, Height = size.Height, Fill = System.Windows.Media.Brushes.White }; vRect.Measure(size); vRect.Arrange(new Rect(size)); rtb.Render(vRect); view.Measure(size); view.Arrange(new Rect(size)); rtb.Render(view); return(rtb); }