TMetaFileCanvas will not work correctly with EMF +, but your task can be completed using GDI +.
Here is an example code that demonstrates how to do this using GDI +:
type
PGdiplusStartupInput = ^TGdiplusStartupInput;
TGdiplusStartupInput = record
GdiplusVersion: Integer;
DebugEventCallback: Integer;
SuppressBackgroundThread: Integer;
SuppressExternalCodecs: Integer;
end;
function GdiplusStartup(var Token: Integer; InputBuf: PGDIPlusStartupInput; OutputBuf: Integer): Integer; stdcall; external 'gdiplus.dll';
procedure GdiplusShutdown(Token: Integer); stdcall; external 'gdiplus.dll';
function GdipCreateFromHDC(DC: HDC; var Graphics: Integer): Integer; stdcall; external 'gdiplus.dll';
function GdipDeleteGraphics(Graphics: Integer): Integer; stdcall; external 'gdiplus.dll';
function GdipLoadImageFromFile(Filename: PWChar; var GpImage: Integer): Integer; stdcall; external 'gdiplus.dll';
function GdipDrawImageRect(Graphics, Image: Integer; X, Y, Width, Height: Single): Integer; stdcall; external 'gdiplus.dll';
procedure LoadEMFPlus(const FileName: WideString; DC: HDC; Width, Height: Integer);
var
Token: Integer;
StartupInput: TGdiplusStartupInput;
Graphics: Integer;
Image: Integer;
begin
StartupInput.GdiplusVersion := 1;
StartupInput.DebugEventCallback := 0;
StartupInput.SuppressBackgroundThread := 0;
StartupInput.SuppressExternalCodecs := 0;
if GdiplusStartup(Token, @StartupInput, 0) = 0 then
begin
if GdipCreateFromHDC(DC, Graphics) = 0 then
begin
if GdipLoadImageFromFile(@FileName[1], Image) = 0 then
begin
GdipDrawImageRect(Graphics, Image, 0, 0, Width, Height);
end;
GdipDeleteGraphics(Graphics);
end;
GdiplusShutdown(Token);
end;
end;
And you can call it like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
LoadEMFPlus('EMFTest.emf',
PaintBox1.Canvas.Handle, PaintBox1.Width, PaintBox1.Height);
end;
Rowan source
share