Solved

Metafile to bitmap conversion

Posted on 1998-08-24
12
2,045 Views
Last Modified: 2013-11-19
We have a Metafile. We want to add some things to the "drawing". Metafile doesn't allow to change ???
Can we convert metafile to bitmap, so possibility to change, and then reconvert to metafile ?
How to do this ?
0
Comment
Question by:jvh042097
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1337495
Hi,
When you convert a graphical metafile to a bitmap, you loose all vector information in the file. So reconverting it to a vectorised metafile is "inpossible".
You can create a metafile DC and do your drawing onto it with normal GDI functions, and then save it as a metafile.
However, look through the common Delphi component/tips&tricks -sites before you get in to deep...
I have done lots of these things in MFC, but will look for something helpful in Delphi as soon as I can. Maybe there are others, GDI-Gurus, who are able to help...

/// John
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337496
jvh?

? you can't change metafile???

Zif.
0
 
LVL 3

Accepted Solution

by:
Matvey earned 100 total points
ID: 1337497
Delphi ALLOWS DRAWING ON METAFILES:

The TMetafileCanvas object allows applications to create metafile images from scratch.
Use TMetafileCanvas as a surface on which to draw a metafile image. When the canvas is destroyed, it transfers the image into the metafile object provided to the canvas constructor. After the image is drawn on the canvas and the canvas is destroyed, the image is 'playable' in the metafile object. For example:

MyMetafile := TMetafile.Create;

with TMetafileCanvas.Create(MyMetafile, 0) do
try
Brush.Color := clRed;
Ellipse(0,0,100,100);
.
finally
Free;
end;
Form1.Canvas.Draw(0,0,MyMetafile); {1 red circle }

To add to an existing metafile image, create a metafile canvas and play the source metafile into the metafile canvas. For example:

{continued from previous example, so MyMetafile contains an image }

with TMetafileCanvas.Create(MyMetafile, 0) do
try
Draw(0,0,MyMetafile);
Brush.Color := clBlue;
Ellipse(100,100,200,200);
.
finally
Free;
end;
Form1.Canvas.Draw(0,0,MyMetafile); {1 red circle and 1 blue circle }

-----------------------------------------------
(Delphi help)

Bye!
Matvey
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337498
jvh?

? you can't change metafile???

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337499
matvey is correct and I was too late to answer :-).

In order to draw on a metafile, you've to create a metafilecanvas.

Regards, Zif.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1337500
Sorry!
I've been debugging a MFC-app all day, so I didn't know what I was talking about. So here's how to do some simple metafile stuff, in Delphi:

var
  mfImage : TMetaFile;
  mfCanvas: TMetaFileCanvas;
  bmTest  : TBitmap;
  ...

  mfImage  := TMetaFile.Create;
  mfImage.LoadFromFile( 'BlahBlah.wmf' );
  mfCanvas := TMetaFileCanvas.Create( mfImage, 0 );
  with mfCanvas do
  begin
    Draw( 0, 0, mfImage );
    Pen.Color := clRed;
    MoveTo( 0, 0 ); // almost any old GDI-command here...
    LineTo( 100, 100 );
    bmTest := TBitmap.Create;
    bmTest.LoadFromFile( 'Blah.bmp' );
    Draw( 0, 0, bmTest );
    bmTest.Free;
  end;
  mfCanvas.Free;
  mfImage.SaveToFile( 'BlahBlahBlah.wmf' );
  mfImage.Free;
 
That's it. Easy, huh?

/// John
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:erajoj
ID: 1337501
Hi Zif,
Just said that converting metafile to bitmap and back again is "impossible", unless you trace the bitmap, of course!

/// John
0
 

Expert Comment

by:aiming
ID: 1337502
hi, My Friend:
  At afternoon, i finished a program about BMP <==> .WMF/.EMF.
  I want use it at COOL 3D. but you want get it. so i'll free all Source.
  ^-^
================
full Source:
================
program bmp2wmf;

uses
  Graphics, SysUtils;

var aBmp : TBitmap;
    aWmf : TMetaFile;
    param1 : String;
    paramExt : String;
begin
  if ParamCount >= 1 then
    begin
      param1 := UpperCase(ParamStr(1));
      paramExt := ExtractFileExt(param1);
    end;
  if (ParamCount < 1) or
     (ParamCount > 2) or
     ((ParamCount = 2) and (UpperCase(ParamStr(2)) <> '/E')) or
     ((paramExt <> '.BMP') and (paramExt <> '.WMF') and (paramExt <> '.EMF')) then
    begin
      writeln('ERROR: Param Invalid!');
      writeln('Uses : Bmp2Wmf <.Bmp/.Wmf> [/E]');
      writeln('notes: Param [/E] Define ouput .EMF File format.');
      exit;
    end;

  aBmp := TBitmap.Create;
  aWmf := TMetaFile.Create;
  if paramExt = '.BMP'
    then
      begin
        aBmp.LoadFromFile(param1);
        aWmf.Width := aBmp.Width;
        aWmf.Height := aBmp.Height;
       if ParamCount = 2 then aWmf.Enhanced := true
                         else aWmf.Enhanced := false;

        with TMetaFileCanvas.CreateWithComment(aWmf,0,'Aiming''s Conver program','V1.0beta') do
          Try Draw(0,0,aBmp);
          Finally Free;
          end;
        if aWmf.Enhanced
          then aWmf.SaveToFile(copy(param1,1,pos('.',param1)) + 'EMF')
          else aWmf.SaveToFile(copy(param1,1,pos('.',param1)) + 'WMF');
      end
    else
      begin
        aWmf.LoadFromFile(param1);
        aBmp.Width := aWmf.Width;
        aBmp.Height := aWmf.Height;
        aBmp.Canvas.Draw(0,0,aWmf);
        aBmp.SaveToFile(copy(param1,1,pos('.',param1)) + 'BMP');
      end;
  aBmp.Destroy;
  aWmf.Destroy;
end.

about me:
================
a programmer from CHINA.
mail: aim_to@yahoo.com
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1337503
Are you aiming at me? ;-)
If you are, then you're missing my point. ;->
What I meant was that converting a bitmapped image into a vectorised image is "impossible". At least not w/o major losses in quality.
I posed a metafile question in the MFC-group a while back, about antialiased VECTORISED metafile to bitmap convertion. Any pointers???

/// John
0
 

Expert Comment

by:aiming
ID: 1337504
to erajoj:
===========
I'm sorry. i can't find the message about antialiased VECTORISED metafile to bitmap convertion at MFC-group.  :-((

a correlative Mail to Ulead Company:
====================================
Dear Kevin Chen and all friends:
  thank you and you help me disposed of this questions about Use WMF/EMF File In COOL  3D  V2.  use Bmp2WMF.EXE translate into file, not include any vector data, It's   impossibility!   of   course, if  I  think Get .BMP Image' bound, I can convert to a vector, but, i think not. :-)
  and  other,  i hope, at COOL 3D' next version, can use .BMP File import. qua a thought,  can  convert  a bound to a vector.  (yes?).   in  the bargain, can disintegrate a WMF vector to many COOL 3D' Object, and these Object can build in a Group or some Groups. i nope future COOL 3D be able to this.
  thank your help. again.
===========================================
hope the mail can help you.
0
 

Author Comment

by:jvh042097
ID: 1337505
thanks for all (!) the answers, but let's stop all discussions now ?

Jan VAN HYFTE
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1337506
Why? Isn't this a forum for discussion and problemsolving? :)

/// John
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

707 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

13 Experts available now in Live!

Get 1:1 Help Now