var getImage = model.Document.Get().Where(x => x.ReferenceNo == ReferenceNo && x.ChecklistId == 225).FirstOrDefault();
var Imagefilename = path + "\\" + ReferenceNo.Replace("/", "_") + "IdentityPhoto.Jpg";
var filebytes = ConvertToBytes(getImage.FileData);
using (var filestream = new FileStream(Imagefilename, FileMode.Create, FileAccess.Write))
{
filestream.Write(filebytes, 0, filebytes.Length);
filestream.Close();
filestream.Dispose();
}
using (Spire.Doc.Document document = new Spire.Doc.Document(@OpenTemplate))
{
Spire.Doc.Section section = document.Sections[0];
System.Drawing.Bitmap p = new Bitmap(System.Drawing.Image.FromFile(@Imagefilename));
// p.RotateFlip(RotateFlipType.Rotate90FlipX);
Spire.Doc.Fields.DocPicture picture = document.Sections[0].Paragraphs[0].AppendPicture(p);
// set image's position
picture.HorizontalPosition = 370.0F;
picture.VerticalPosition = 480.0F;
// set image's size
picture.Width = 80;
picture.Height = 80;
// set textWrappingStyle with image;
picture.TextWrappingStyle = Spire.Doc.Documents.TextWrappingStyle.Through;
// Save doc file.
document.SaveToFile(@Demo, Spire.Doc.FileFormat.Docx);
p.Dispose();
document.Dispose();
document.Close();
}
// Trying to delete this file
try
{
if (File.Exists(Imagefilename))
{
File.Delete(Imagefilename);
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
Using a Word document as template, image is retrieved from database. It is stored on the C drive, the image at that location is then being used and inserted into a word document.
When trying to delete the image from the drive after use it keeps on throwing an error:
Error message "process cannot access the file because it is being used by another process"
I have tried using .Dispose
and .Close
functions and using statements. I have tried Directory.Delete
but nothing seems to be working.