• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

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.
0
svvada
Asked:
svvada
  • 7
  • 3
2 Solutions
 
shaneholmesCommented:
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
0
 
shaneholmesCommented:
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
0
 
shaneholmesCommented:
Also, as for the MetaFile you created - did you set its Enhanced property to false?

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

ShaneHolmes
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
svvadaAuthor Commented:
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.
0
 
shaneholmesCommented:
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
0
 
shaneholmesCommented:
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
0
 
svvadaAuthor Commented:
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;
0
 
shaneholmesCommented:
Oh ok!

Well, I dont think you are going to get any smaller. Unless of course you perform some type of compression or resizing on the bitmap before you copy it to the MetaFile. MetaFiles  (like bitmaps) are much larger image formats than say a JPEg or a GIF.

Why can't you use a JPEG versus a metafile?

Shane
0
 
Slick812Commented:
hello svvada, you do not seem to know what the difference is between the way a Bitmap image uses and saves it's  image data and how a Meta File image uses and saves it's image data. A Bitmap file is called a "Raster" image, which means that the "image data" is a color definition of EVERY PIXEL in that bitmap image, if a bitmap is 32x32 pixels then there will be 1024 color values in the image data for each of the 1024 pixels in that bitmap. . .

A meta file image is called a "Vector" image, which means that there are NO PIXEL color definitions, like a bitmap has, instead for it's image data there are drawing functions (API functions like MoveToEx( ) and LineTo( ) ) recorded into the file (vector drawing), so if there are a few drawing functions , like a TChart drawing) you will get a small file size. . . . . . However if you paint a Raster Image, like a bitmap onto a vector image like a meta file then the meta file becomes a rastor image, I.E. the drawing functions recorded for the bitmap image data is a function for every pixel, which may result in the same size (in bytes) or larger than the bitmap. .  For file size reasons, you should not draw a bitmap file on a meta file. . .
You might should use a JPEG image for reduction in file size for a bitmap, as shaneholmes  has already sugested.
0
 
shaneholmesCommented:
Perfectly Stated - Slick812 - I think you even helped me better understand something... thanks!

ShaneHolmes
0
 
svvadaAuthor Commented:
Thanks Slick812 and Shane, this has helped my understanding of how this works.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now