Solved

Colouring Metafiles

Posted on 1998-06-19
5
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:Socrates050697
ID: 1354055
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
ID: 1354056
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
ID: 1354057
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
ID: 1354058
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
ID: 1354059
Thats brilliant.  It works a treat.

Cheers

BigMadDrongo
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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