0

I am using Delphi XE3. I try to open a WMF file, with the following code:

    var
        Picture: TPicture;
    begin
        Picture := TPicture.Create();

        Picture.LoadFromFile('E:\temp\thumbnail.wmf');

        Picture.Free;
    end;    

But I will always get invalid graphic exception. I try to open the wmf file with other tools such as ACDSee and confirm the image is completed OK. Why Delphi cannot open it. I have tried different versions of Delphi but all fails.

The wmf file can be downloaded at https://www.dropbox.com/s/wcqdma42xlra07p/thumbnail.wmf?dl=0

Thanks

tempc
  • 1
  • 1
  • I think `TPicture` is intended for raster images. Can you open it using `TMetafile`? – David Heffernan Jan 08 '19 at 09:42
  • I debug the code. TPicture is just using TMetafile to open the file, but fails. – tempc Jan 08 '19 at 09:52
  • Yup, it should work, but it doesn't. I don't know of specific features that are or are not supported. I checked the first couple of [bytes of the header](https://www.fileformat.info/format/wmf/egff.htm) and those seem to be okay, at least. Unfortunately Delphi doesn't tell you in detail why it doesn't work... – GolezTrol Jan 08 '19 at 10:10

2 Answers2

3

Delphi can only read placeable WMF metafiles that start with a WMF Placeable Header Record. The given file just is not a placeable WMF metafile.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
0

TPicture uses the VCL's TMetafile class by default to load WMF files. But, as Uwe's answer stated, TMetafile can only load placeable WMF metafiles 1, and your file is not a placeable WMF metafile 2.

Even the VCL's TWICImage class, which is a wrapper for Microsoft's WIC (Windows Imaging Component) API that can load many image formats supported by Windows, can't load your file either, and that failure comes from the OS itself, not from the VCL. However, WIC is extensible, so you could try implementing and registering a custom decoder for handle your WMF file. The Metafile format is documented on MSDN.

Though, WMF is really a legacy image format from the 16-bit days. If you really want to work with metafiles, you should use EMF instead, which TMetafile also supports.

1: a placeable WMF metafile contains an extra header to describe the XY coordinates where the image should be drawn on a GDI canvas, and the number of logical units per inch for scaling the image.

2: Your file identifies itself as a non-placeable metafile in MEMORYMETAFILE format rather than DISKMETAFILE format. I wonder if that has anything to do with your issue?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770