(Thanks to Arnaud for his comments.)
When working with matafiles, only the bitmap data will be rendered when you load the file unless you use the Windows GDI methods to render (or play) the vector content. I use a 3rd party library (GDI+) to do the work for me. I also use Graphics32 whenever I work with images.
GDI+
The author's website doesn't seem to have the source code anymore. However, I found it here by searching for gdipobj.pas
. I found it on code.google.com
as well (included in several projects, like this one).
Graphics32
The source for Graphics32 is on SourceForge here. The SVN command is:
svn co https://graphics32.svn.sourceforge.net/svnroot/graphics32/trunk graphics32
My Solution
It took a while to figure out how to properly render the Metafile, at the right size, etc. Here is the function that I use.
uses
GR32, GDIPOBJ, ActiveX;
procedure RenderWMF(var WMF: TMemoryStream; OutputBitmap: TBitmap32; const ScreenResolution: Integer = 96);
var
gph: TGPGraphics;
img: TGPImage;
bmap: TBitmap;
ms: IStream;
pLi: PLongint;
iPos: Int64;
tmpms: TMemoryStream;
begin
WMF.Position := 0;
ms := TStreamAdapter.Create(WMF, soReference);
try
pLi := nil;
ms.Write(WMF.Memory, WMF.Size, pLi);
bmap := TBitmap.Create;
try
ms.Seek(0, soFromBeginning, iPos);
img := TGPImage.Create(ms);
try
bmap.width := Trunc(img.GetWidth / img.GetHorizontalResolution * ScreenResolution + 0.5);
bmap.height := Trunc(img.GetHeight / img.GetVerticalResolution * ScreenResolution + 0.5);
gph := TGPGraphics.Create(bmap.Canvas.Handle);
try
gph.DrawImage(img, 0, 0, bmap.Width, bmap.Height);
tmpms := TMemoryStream.Create;
try
bmap.SaveToStream(tmpms);
tmpms.Position := 0;
OutputBitmap.LoadFromStream(tmpms);
finally
tmpms.Free;
end;
finally
gph.Free;
end;
finally
img.Free;
end;
finally
bmap.Free;
end;
finally
ms := nil;
end;
end;
I call it with code like this:
var
bm32: TBitmap32;
wmf: TMemoryStream;
begin
bm32 := TBitmap32.Create;
try
wmf := TMemoryStream.Create;
try
wmf.LoadFromFile('metafile.emf');
RenderWMF(wmf, bm32, Screen.PixelsPerInch);
// Do something with 'bm32'
finally
wmf.Free;
end;
finally
bm32.Free;
end;
end;
I know this answer is much broader than how to properly set the canvas size
. I hope it is helpful.