插入特定位置的word文档后无法删除图像c#& spire.doc

时间:2017-02-08 06:23:11

标签: c# image ms-word office-interop spire.doc

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但似乎没有任何效果。

0 个答案:

没有答案