private void GridScreenshot() { try { var width = (int)MainGrid.ActualWidth + 10; var height = (int)MainGrid.ActualHeight + 10; var renderBitmap = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Pbgra32); MainGrid.Measure(new Size(width, height)); MainGrid.Arrange(new Rect(new Size(width, height))); renderBitmap.Render(MainGrid); var pngImage = new PngBitmapEncoder(); pngImage.Frames.Add(BitmapFrame.Create(renderBitmap)); using (var fileStream = File.Create($@"C:\Users\LPI\Desktop\souk_again\yo\{DateTime.Now.ToString("yyyyMMddhhmmss")}.jpg")) { pngImage.Save(fileStream); } } catch (Exception ex) { MessageBox.Show("Error while screenshoting : " + ex.Message); } }
private void SaveToFile() { int height = (int)MainGrid.Height; int width = (int)MainGrid.Width; //BitmapSource bmp = CreateBitmapFromVisual(width, height, MainGrid, true); Size size = MainGrid.RenderSize; RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); MainGrid.Measure(size); MainGrid.Arrange(new Rect(size)); bmp.Render(MainGrid); string file = "test.png"; // this.txtFilename.Text; string Extension = System.IO.Path.GetExtension(file).ToLower(); BitmapEncoder encoder; if (Extension == ".gif") { encoder = new GifBitmapEncoder(); } else if (Extension == ".png") { encoder = new PngBitmapEncoder(); } else if (Extension == ".jpg") { encoder = new JpegBitmapEncoder(); } else { return; } encoder.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(file)) { encoder.Save(stm); } }