/// <summary> /// Resize and export the bitmap image as pdf /// </summary> private void ExportToPdf(int width, int height) { //get the bitmap of the requested sizes Bitmap btm = Printprimitives(width, height); try { //new pdf doc PdfDocument doc = new PdfDocument(); doc.Pages.Add(new PdfPage()); XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); XImage img; //get the bitmap and save it as pdf doc string tempfile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName().Replace(".", "") + ".bmp"); btm.Save(tempfile); img = XBitmapImage.FromFile(tempfile); string name = Path.GetRandomFileName() + ".pdf"; xgr.DrawImage(img, 0, 0); doc.Save(name); doc.Close(); btm.Dispose(); img.Dispose(); MessageBox.Show($"File saved in the app directory with name {name}"); //delete temp file File.Delete(tempfile); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static XImage ImageToByte2(RenderTargetBitmap renderTargetBitmap) { var bitmapImage = new BitmapImage(); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); using (var stream = new MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); return(XBitmapImage.FromStream(stream)); } }