public void Print(System.Windows.Controls.Image image) { System.Printing.LocalPrintServer localPrintServer = new System.Printing.LocalPrintServer(); PrintDialog printDialog = new PrintDialog(); System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / image.ActualWidth, capabilities.PageImageableArea.ExtentHeight/image.ActualHeight); image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale); System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); image.Measure(sz); image.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); printDialog.PrintVisual(image, "Print Image"); }
private void SaveImageToFile(System.Windows.Controls.Image imageIn, string filePath) { if (imageIn.Source == null) { return; } RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)imageIn.Source.Width, (int)imageIn.Source.Height, 96.0, 96.0, PixelFormats.Pbgra32); imageIn.Measure(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height)); imageIn.Arrange(new Rect(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height))); rtBmp.Render(imageIn); JpegBitmapEncoder jEncoder = new JpegBitmapEncoder(); MemoryStream stream = new MemoryStream(); jEncoder.Frames.Add(BitmapFrame.Create(rtBmp)); /* PngBitmapEncoder encoder = new PngBitmapEncoder(); MemoryStream stream = new MemoryStream(); encoder.Frames.Add(BitmapFrame.Create(rtBmp)); */ // Save to memory stream and create Bitamp from stream jEncoder.Save(stream); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream); // Demonstrate that we can do something with the Bitmap bitmap.Save(GetSaveToFilePath(filePath), ImageFormat.Jpeg); // Optionally, if we didn't need Bitmap object, but // just wanted to render to file, we could: //encoder.Save(new FileStream(GetSaveToFilePath(filePath), FileMode.Create)); }