Ștefan Blaga Ștefan Blaga - 8 days ago 5
C# Question

ZedGraph doesn't dispose emf file after create it

I have this code below:

static internal bool SaveEnhMetafileToFile(Metafile mf, string fileName)
{
bool bResult = false;
IntPtr hEMF;
hEMF = mf.GetHenhmetafile(); // invalidates mf
if (!hEMF.Equals(new IntPtr(0)))
{
StringBuilder tempName = new StringBuilder(fileName);
CopyEnhMetaFile(hEMF, tempName);
DeleteEnhMetaFile(hEMF);

}
return bResult;
}


When
CopyEnhMetaFile(hEMF, tempName);
is called, the picture is created but after calling
DeleteEnhMetaFile(hEMF);
function I cannot delete the picture because it is used by my program (
vshost.exe
).
The program is created in C#

Answer

As stated in the link I posted - you need to delete the handle created by CopyEnhMetaFile :

static internal bool SaveEnhMetafileToFile(Metafile mf, string fileName)
{
  bool bResult = false;
  IntPtr hEMF;
  hEMF = mf.GetHenhmetafile(); // invalidates mf 
  if (!hEMF.Equals(new IntPtr(0)))
  {
    StringBuilder tempName = new StringBuilder(fileName);
    IntPtr hCopyEMF = CopyEnhMetaFile(hEMF, tempName.ToString());
    DeleteEnhMetaFile(hCopyEMF);
    DeleteEnhMetaFile(hEMF);
  }
return bResult;
}