2010-04-21 93 views
3

我有一個應用程序,可以加載各種圖形文件格式,如bmp,jpg,png,emf等...並將它們呈現在屏幕上進行預覽。德爾福7和EMF +文件

我正在使用Delphi 7.我剛剛介紹了在使用GDIPlus.dll創建的Windows XP時創建的EMF +文件格式。我可以用Windows圖片瀏覽器打開EMF +文件,圖像的質量非常高,可以放大和縮小,而不會有任何模糊。

問題是我似乎無法找到一種方法(在Delphi 7中)打開此文件格式並將其呈現在屏幕上。有沒有人知道可以在Delphi 7中使用的代碼示例或組件來呈現EMF +文件?

回答

3

您可以使用具有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

還沒有親自去查看,但看起來合適的工作。

+0

這是普通的舊EMF,你肯定也不會EMF + ? Afaik Delphi在股票發行版中沒有GDIplus組件 – 2010-04-22 07:52:12

+0

嗯,不知道:) – Runner 2010-04-22 08:05:38

+0

更新了答案。 – Runner 2010-04-22 08:17:38

4

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;