The cause for it seems not to be the 'Load' procedure but thomething else I did not yet find out.
I'm using nearly the same code as in PrintPreview and it runs well.
Here follows an extraction of MY code:
/
- Code: Select all
/Load procedure of the EMF file:
//.........
if FileExists(sMetaPathFileName) then begin
//Picture Objekt für MetaFile erzeugen
tPIC := tPicture.Create;
//MetaFile Objekt erstellen
tEMF := TMetaFile.Create;
tEMF.Handle := ReadMetafileFromFile(sMetaPathFileName);
//.........
//Now in detail the read procedure:
//------------------------------------------------------------------------------
function TImageForm.ReadMetafileFromFile (sMetaPathFileName: String):HENHMETAFILE;
// Liest ein WMF (inkl. Placable Meta File) oder ein EMF-Format - Reads a WMF (including placeable Metafiles) or EMF from a file
//und gibt das Handle zum EMF-Format zurück. - and returns a handle to an enhanced Metafile.
var pFileName: array [0..MAX_PATH] of char;
hMetafile: HENHMETAFILE;
fileStream: TFileStream;
begin
StrPCopy (pFileName, sMetaPathFileName);
hMetafile := GetEnhMetaFile (pFileName);
// convert WMF to EMF if necessary
if hMetafile = 0 then
begin
fileStream := TFileStream.Create (sMetaPathFileName, fmOpenRead);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//The following is comparable with the correspondent code line in PrintPreview:
hMetafile := ReadWMFfromStream (fileStream, fileStream.size);
//Here, it runs well, but in PrintPreview, it fails.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fileStream.Free;
end;
result := hMetafile;
end;
//------------------------------------------------------------------------------
function TImageForm.ReadWMFfromStream(Stream: TStream; Length: integer): HENHMETAFILE;
// Lesen des alten WMF Formats. - Reads an old WMF metafile from a stream
type
//Header des placable Metafile hat 22 Byte. - Header for placeable Metafile, 22 byte in size
METAFILEHEADER = packed record
key: DWORD;
hmf: WORD;
left: integer;
top: integer;
right: integer;
bottom: integer;
inch: WORD;
reserved: DWORD;
checksum: WORD;
end;
var
WMF: MetaFileHeader;
BitMem: Pointer;
MFP: TMetaFilePict;
width: integer;
height: integer;
dx: integer;
dy: integer;
begin
// Den Header lesen. - Read in Header
Stream.Read(WMF, SizeOf(WMF));
// Test, ob placable Metafile gefunden. - check if it is a placeable metafile
if (WMF.key = $9AC6CDD7) then
begin
// Placable Metafile gefunden. - placeable Metafile found
Dec(Length, SizeOF(WMF));
// WMF Abmessungen in MM_HIENGLISH abrufen. - calculate WMF size in MM_HIENGLISH units
if WMF.Inch = 0 then WMF.Inch := 96;
dx := WMF.Right - WMF.Left;
dy := WMF.Bottom - WMF.Top;
if dx < 0 then dx := 0;
if dy < 0 then dy := 0;
height := MulDiv (dy, 2540, WMF.Inch);
width := MulDiv (dx, 2540, WMF.Inch);
with MFP do
begin
MM := MM_HIENGLISH;
xExt := width;
yExt := height;
hmf := 0;
end;
end
else begin
// Normales altes WMF Format - normal old WMF Metafile
Stream.Seek (soFromBeginning ,0);
with MFP do
begin
MM := MM_HIENGLISH; //MM_HIMETRIC;
xExt := 0;
yExt := 0;
hmf := 0;
end;
end;
try
GetMem(Bitmem, Length);
Stream.Read(BitMem^, Length);
result := SetWinMetaFileBits(Length, BitMem, 0, MFP);
finally
Freemem(BitMem, Length);
end;
end;
Attached You find an EMF '001.EMF' which was extracted from the SPL spool file of my virtual printer code. It is displayable with any viewer and the standard File for my current print monitor made for net printing.
by the way, I replaced the corresponding code in PrintPreview just with the code above. The result is again a crash at the SAME code line as shown above between the two lines with exclamations. I don't want to spend much time finding the bug inside PrintPreview because there exists someone else knowing it much better than me.