Solved

Colouring Metafiles

Posted on 1998-06-19
5
408 Views
Last Modified: 2010-04-04
I have a black and white metafile, and I would like to play it onto a canvas using different colours (i.e. red on white, blue on white etc.)  Is this possible.  At the moment I'm playing it onto a bitmap and changing the colours pixel by pixel, but this is slower than a snail whose been nailed down.  Any ideas?

Cheers

BigMadDrongo
0
Comment
Question by:BigMadDrongo
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:Socrates050697
Comment Utility
I've never done it but this may work :-

Use EnumEnhMetaFile to go through the metafile records. What this does is call a CallBack function that you've written, sending it each metafile record in turn. Your CallBack function then process the record as it seems fit. ie you can change the colours if you like.

Socrates.
0
 
LVL 3

Expert Comment

by:vladika
Comment Utility
Try this procedure.
It's replace Src color by Dst color

procedure ReplaceColor(Metafile: TMetafile; Src, Dst: TColor);
var ColorBmp, MaskBmp: TBitmap;
    R: TRect;
begin
  R := Rect(0, 0, Metafile.Width, Metafile.Height);
  ColorBmp := TBitmap.Create;
  try
    ColorBmp.Width := R.Right;
    ColorBmp.Height := R.Bottom;
    ColorBmp.Canvas.Brush.Color := Dst;
    ColorBmp.Canvas.FillRect(R);
    MaskBmp := TBitmap.Create;
    try
      MaskBmp.Width := R.Right;
      MaskBmp.Height := R.Bottom;
      MaskBmp.Canvas.Draw(0, 0, Metafile);
      MaskBmp.Mask(Src);
      with TMetafileCanvas.Create(Metafile, 0) do
      try
        Draw(0, 0, Metafile);
        BitBlt(ColorBmp.Canvas.Handle, 0, 0, R.Right, R.Bottom, MaskBmp.Canvas.Handle, 0, 0, SRCAND);
        BitBlt(MaskBmp.Canvas.Handle, 0, 0, R.Right, R.Bottom, MaskBmp.Canvas.Handle, 0, 0, DSTINVERT);
        BitBlt(Handle, 0, 0, R.Right, R.Bottom, MaskBmp.Canvas.Handle, 0, 0, SRCAND);
        BitBlt(Handle, 0, 0, R.Right, R.Bottom, ColorBmp.Canvas.Handle, 0, 0, SRCPAINT);
      finally
        Free;
      end;
    finally
      MaskBmp.Free;
    end;
  finally
    ColorBmp.Free;
  end;
end;

// ---------- end -------

How to use:
Let MF is your metafile

  ReplaceColor(MF, clBlack, clRed); // replace Black color on Red color

0
 

Author Comment

by:BigMadDrongo
Comment Utility
It's good, and it works, but it isn't any faster than changing the colour pixel by pixel in a bitmap.  I really need something faster.

Cheers

BigMadDrongo
0
 
LVL 1

Accepted Solution

by:
Socrates050697 earned 100 total points
Comment Utility
I've worked out how to use EnumEnhMetaFile :-

The code below seems to work fine for changing the pen colour. If you want to change any other parts of the metafile you'll have to add some other cases to the CASE statement in ENUMCALLBACK. Theres a list of all the metafile record commands in the API help supplied with delphi.

The BUTTON2CLICK function just draws the metafile (called MYMETAFILE) onto the canvas of the form. You'll have to change the first parameter of EnumEnhMetaFile to the handle whatever canvas you need to draw on.

procedure TForm1.Button2Click(Sender: TObject);
var
  MyRect: TRect;
begin
  SetRect(MyRect,0,0,ClientWidth,ClientHeight);
  if not EnumEnhMetaFile(Canvas.Handle,MyMetaFile.Handle,@EnumCallBack,@MyRect,MyRect) then
    ShowMessage('Failed');
end;

function EnumCallBack(HDC: integer;var pHTable: THandleTable;var pEMFR: TEnhMetaRecord;nObj: integer;lpData: pointer): integer; stdcall;
var
  MyPen: Integer;
begin
  Case pEMFR.iType of
    EMR_CREATEPEN : begin
      MyPen:=CreatePen(PS_SOLID,10,clRed);
      DeleteObject(SelectObject(HDC,MyPen));
    end;
    else PlayEnhMetaFileRecord(HDC,phTable,pEMFR,nObj);
  end;
  result:=1;
end;

Socrates

0
 

Author Comment

by:BigMadDrongo
Comment Utility
Thats brilliant.  It works a treat.

Cheers

BigMadDrongo
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now