I'm trying to add a picture of part of a form to a PDF so I'm using PDFsharp, but one of the problems I have is that I can't delete the picture after I have added it to the PDF file.
This is the code I'm using:
private void button12_Click(object sender, EventArgs e)
{
string path = @"c:\\Temp.jpeg";
Bitmap bmp = new Bitmap(314, this.Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
GeneratePDF("test.pdf",path);
var filestream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
filestream.Close();
if (File.Exists(path))
{
File.Delete(path);
}
}
private void GeneratePDF(string filename, string imageLoc)
{
PdfDocument document = new PdfDocument();
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
// Save and start View
document.Save(filename);
Process.Start(filename);
}
void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
XImage image = XImage.FromFile(jpegSamplePath);
gfx.DrawImage(image, x, y, width, height);
}
The FileStream
is there because I was trying to make sure that it was closed and if that was the problem.
This is where I got the code for writing the picture to the PDF overlay-image-onto-pdf-using-pdfsharp and this is where I got the code to make a picture of the form capture-a-form-to-image
Did I miss something obvious?
Or is there a better way to do this?