Solved

Colouring Metafiles

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

16 Experts available now in Live!

Get 1:1 Help Now