Delphi 7 and EMF + Files

I have an application that can download various graphic file formats, such as bmp, jpg, png, emf, etc ... and display them on the screen for preview.

I am using Delphi 7. I have just been presented in the EMF + file format, which was created during Windows XP, which is created using GDIPlus.dll. I can open EMF + files using the Windows Picture Viewer, and the image is very tall, and can zoom in and out without any blur.

The problem is that I cannot find a way (in Delphi 7) to open this file format and display it on the screen. Does anyone know a sample code or component that can be used in Delphi 7 to render an EMF + file?

+3
source share
2 answers

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;
+4
source

You can use "TMetaFileCanvas" with EMF support. Code snippet:

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;

This way you can load EMF, draw EMF and save it. But presenting it as vector graphics from Delphi is another problem. Delphi only works with raster graphics out of the box. But, as I understand it, you only want to read and draw. To convert it to BMP, you can:

// destroy canvas to transfer the image into the metafile object
FreeAndNil(MyCanvas);
// draw image as normal graphic
BMP.Canvas.Draw(0, 0, MyMetaFile);

EDIT:

, TMetaFileCanvas, , EMF+. , .

, , , .

http://blog.synopse.info/post/2010/04/02/Antialiased-drawing-from-TMetaFile

:

http://synopse.info/files/SynGdiPlus.zip

Havent , .

+3

Source: https://habr.com/ru/post/1742153/


All Articles