0
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);
}

使用 Word 文档作为模板,从数据库中检索图像。它存储在 C 驱动器上,然后使用该位置的图像并将其插入到 Word 文档中。

使用后尝试从驱动器中删除图像时,它不断抛出错误:

错误消息“进程无法访问该文件,因为它正被另一个进程使用”

我尝试过使用.Dispose.Close函数以及使用语句。我已经尝试过Directory.Delete,但似乎没有任何效果。

4

0 回答 0