Link to home
Start Free TrialLog in
Avatar of svvada
svvada

asked on

Save form to metafile

I'm trying to save my form to a metafile.
I've tried just creating a Tmetafile  and using GetFormImage to accuire the form which worked just fine. But the file size is the same as if I just save it as a bitmap. So my real question is how can I get the metafile smaller.
Avatar of shaneholmes
shaneholmes

Try creating it as a JPEG if you want it small.

var
 DestR : TRect;
 Bmp: TBitmap;
 Jpg: TJpegImage;
begin
  Bmp:= TBitmap.Create;
  Bmp.Width := Self.ClientRect.Right - Self.ClientRect.Left;
  Bmp.Height := Self.ClientRect.Bottom - Self.ClientRect.Top;
  DestR := Rect(0,0,Bmp.Width , Bmp.Height);
  Bmp.Canvas.CopyRect(DestR, Canvas, Self.ClientRect);
  Jpg := TJpegImage.Create;
  JPg.CompressionQuality:= ?                  
  Jpg.Assign(ABitmap);
  Jpg.SaveToFile('C:\Test.jpg');
end;

ShaneHolmes
Sorry, make sure you place in the Uses section - JPEG

procedure TForm1.Button1Click(Sender: TObject);
var
 DestR : TRect;
 Bmp: TBitmap;
 Jpg: TJpegImage;
begin
  Bmp:= TBitmap.Create;
  Bmp.Width := Self.ClientRect.Right - Self.ClientRect.Left;
  Bmp.Height := Self.ClientRect.Bottom - Self.ClientRect.Top;
  DestR := Rect(0,0,Bmp.Width , Bmp.Height);
  Bmp.Canvas.CopyRect(DestR, Canvas, Self.ClientRect);
  Jpg := TJpegImage.Create;
  JPg.CompressionQuality:= 70;
  Jpg.Assign(Bmp);
  Jpg.SaveToFile('C:\Test.jpg');
end;

When I tested this, the Bitmap was 2,041 KB and the JPEG was 13 KB


ShaneHolmes
Also, as for the MetaFile you created - did you set its Enhanced property to false?

M:= TMetaFile.Create;
M.Enhanced:= False;

ShaneHolmes
Avatar of svvada

ASKER

ShaneHolmes,
Thanks for the input but I want to make a metafile, and I want to save it as both types (emf and wmf). The TChart component has the functionality I'm looking for, but I haven't got the source for that. The bitmap file generated by TChart is 391KB while emf is 4KB and wmf is 6KB.
Can you post your code, so we can see where you are going wrong....?
Also, did you see my last wquestion above - please reply so i know you .... thanks
I can't seemt o get the Metafile the same size as the bmp.
Its always larger

Metafile = 3,585 KB
Bitmap = 2,041 KB



procedure TForm1.Button1Click(Sender: TObject);
var
 DestR : TRect;
 Bmp: TBitmap;
 MetaFile       : TMetaFile;
 MetaFileCanvas : TMetaFileCanvas;
begin
 Bmp:= TBitmap.Create;
 Bmp.Width := Self.ClientRect.Right - Self.ClientRect.Left;
 Bmp.Height := Self.ClientRect.Bottom - Self.ClientRect.Top;
 DestR := Rect(0,0,Bmp.Width , Bmp.Height);
 Bmp.Canvas.CopyRect(DestR, Canvas, Self.ClientRect);
 Bmp.SaveToFile('C:\Test.bmp');
 MetaFile := TMetaFile.Create;
 MetaFile.Width:= BMp.Width;
 MetaFile.Height:= Bmp.Height;
 MetaFileCanvas := TMetaFileCanvas.Create(MetaFile, 0);
 MetaFileCanvas.Draw(0,0, Bmp);
 MetaFileCanvas.Free;
 //MetaFile.Enhanced := False; {Optional}
 MetaFile.SaveToFile('C:\test.wmf'); {Optional}
 MetaFile.Free;
end;

Id be interesed in seeing your code where your metafile is the same size as the bitmap

Shane
Avatar of svvada

ASKER

OK here's my code, it's the enhanced file (emf) that is the same size as the bitmap, the wmf is larger that's probably what you're doing right?

procedure TForm1.bitmap2emf;
var
  Metafile: TMetafile;
  MetaCanvas: TMetafileCanvas;
  Bitmap : TBitmap;
begin
  Metafile := TMetaFile.Create;
  try
      BitMap := GetFormImage;
      try
      Metafile.Height := BitMap.Height;
      Metafile.Width := BitMap.Width;
      MetaCanvas := TMetafileCanvas.Create(Metafile, 0);
      try
        MetaCanvas.Draw(0, 0, GetFormImage);
      finally
        MetaCanvas.Free;
      end;
    Metafile.SaveToFile('e:\test.emf');
    Metafile.Enhanced := False;
    Metafile.SaveToFile('e:\test.wmf');
    BitMap.SaveToFile('e:\test.bmp');
    finally
      Bitmap.Free;
    end;
  finally
    Metafile.Free;
  end;
end;
SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfectly Stated - Slick812 - I think you even helped me better understand something... thanks!

ShaneHolmes
Avatar of svvada

ASKER

Thanks Slick812 and Shane, this has helped my understanding of how this works.