// get the image from the clipboard Image img = Clipboard.GetImage(); // create a blank PDF document PdfDocument doc = new PdfDocument(); //Add a page to the document. PdfPage page = doc.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Load the image from the object (obtained from clipboard). PdfBitmap image = new PdfBitmap(img); float PageWidth = page.Graphics.ClientSize.Width; float PageHeight = page.Graphics.ClientSize.Height; float myWidth = image.Width; float myHeight = image.Height; float shrinkFactor; if (myWidth > PageWidth) { shrinkFactor = myWidth / PageWidth; myWidth = PageWidth; myHeight = myHeight / shrinkFactor; } if (myHeight > PageHeight) { shrinkFactor = myHeight / PageHeight; myHeight = PageHeight; myWidth = myWidth / shrinkFactor; } // give us a 10mm margin float XPosition = 10; // (PageWidth - myWidth) / 2; float YPosition = 10; // (PageHeight - myHeight) / 2; //Draw the image graphics.DrawImage(image, XPosition, YPosition, myWidth, myHeight); try { SaveFileDialog sd = new SaveFileDialog() { DefaultExt = "*.pdf", Filter = "PDF Files (*.pdf)|*.pdf", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), FileName = "copiedImage.pdf", Title = "Save screenshot to...", OverwritePrompt = true, CheckPathExists = true }; if (sd.ShowDialog() == DialogResult.OK) { // save our document stream to our file name //Save the document. doc.Save(sd.FileName); } } catch (Exception ex) { //sl.Error("Save - Error => " + ex.Message); //throw; } //Close the document. doc.Close(true); |