Solved

Metafile to bitmap conversion

Posted on 1998-08-24
12
2,055 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
In this Micro Tutorial viewers will learn how to remove an unwanted object using Photoshop’s feature known as content-aware fill.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

786 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