德尔福7和EMF +文件
问题描述:
我有一个应用程序,可以加载各种图形文件格式,如bmp,jpg,png,emf等...并将它们呈现在屏幕上进行预览。德尔福7和EMF +文件
我正在使用Delphi 7.我刚刚介绍了在使用GDIPlus.dll创建的Windows XP时创建的EMF +文件格式。我可以用Windows图片浏览器打开EMF +文件,图像的质量非常高,可以放大和缩小,而不会有任何模糊。
问题是我似乎无法找到一种方法(在Delphi 7中)打开此文件格式并将其呈现在屏幕上。有没有人知道可以在Delphi 7中使用的代码示例或组件来呈现EMF +文件?
答
您可以使用具有EMF支持的“TMetaFileCanvas”。代码片段:
procedure TForm1.Button1Click(Sender: TObject);
var
MyMetaFile: TMetaFile;
MyCanvas: TMetafileCanvas;
begin
MyMetaFile:= TMetaFile.Create;
try
MyMetaFile.LoadFromFile('C:\example.emf');
MyCanvas:= TMetafileCanvas.Create(MyMetaFile, 0);
try
MyCanvas.Draw(0, 0, MyMetaFile);
MyCanvas.Pen.Color:= clRed;
MyCanvas.MoveTo(0, 0);
MyCanvas.LineTo(100, 100);
MyCanvas.Free;
Image1.Canvas.Draw(0, 0, MyMetaFile);
finally
MyCanvas.Free;
end;
MyMetaFile.SaveToFile('C:\example.emf');
finally
MyMetaFile.Free;
end;
end;
这种方式可以加载EMF,绘制到EMF并保存。但将它作为Delphi的矢量图形呈现是另一个问题。德尔福只适用于开箱即用的位图图形。但据我所知,你只想阅读和绘制它。将其转换为BMP比如,你可以这样做:
// destroy canvas to transfer the image into the metafile object
FreeAndNil(MyCanvas);
// draw image as normal graphic
BMP.Canvas.Draw(0, 0, MyMetaFile);
编辑:
马可亲切与EMF +中指出TMetaFileCanvas可能woun't正常工作。没有测试过,所以我无法确认。
但似乎有一个单位,与此一起工作。
http://blog.synopse.info/post/2010/04/02/Antialiased-drawing-from-TMetaFile
下载请联系:
http://synopse.info/files/SynGdiPlus.zip
还没有亲自去查看,但看起来合适的工作。
答
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;
这是普通的旧EMF,你肯定也不会EMF + ? Afaik Delphi在股票发行版中没有GDIplus组件 – 2010-04-22 07:52:12
嗯,不知道:) – Runner 2010-04-22 08:05:38
更新了答案。 – Runner 2010-04-22 08:17:38