Solved

Colouring Metafiles

Posted on 1998-06-19
5
430 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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