我有一个应用程序可以加载各种图形文件格式,如bmp,jpg,png,emf等…并将它们渲染到屏幕上进行预览. 我正在使用Delphi 7.我刚刚介绍了使用GDIPlus.dll创建的Windows XP时创建的EMF文件格式.我可以
我正在使用Delphi 7.我刚刚介绍了使用GDIPlus.dll创建的Windows XP时创建的EMF文件格式.我可以使用Windows Picture Viewer打开EMF文件,图像质量非常高,可以放大和缩小而不会有任何模糊.
问题是我似乎无法找到一种方法(在Delphi 7中)打开这种文件格式并在屏幕上呈现它.有没有人知道可以在Delphi 7中使用的代码示例或组件来呈现EMF文件?
TMetaFileCanvas无法与EMF一起正常工作,但您的任务可以使用GDI完成.以下是一些示例代码,演示了如何使用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;
你可以这样称呼它:
procedure TForm1.Button1Click(Sender: TObject);
begin
LoadEMFPlus('EMFTest.emf',
PaintBox1.Canvas.Handle, PaintBox1.Width, PaintBox1.Height);
end;
