Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Colouring Metafiles

Posted on 1998-06-19
5
Medium Priority
?
452 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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

618 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