Thursday, April 29, 2010

Converting WMF files to JPG

Recently, I had call to convert .WMF files to .JPG.  Now there are any number of commercial and shareware solutions to this problem, but as a programmer, I wanted to do this myself.  This turns out to be fairly easy using C++Builder.  Here is the code:

TMetafile * mf = new TMetafile(); 
TJPEGImage * jpeg = new TJPEGImage();
Graphics::TBitmap * bmp = new Graphics::TBitmap();
mf->LoadFromFile("c:\\testdata\\sample.wmf");
bmp->SetSize(mf->Width,mf->Height);
bmp->Canvas->Draw(0,0,mf);
jpeg->Assign(bmp);
jpeg->CompressionQuality = 50;
jpeg->SaveToFile("c:\\testdata\\sample.jpg");
delete mf;
delete bmp;
delete jpeg;
And, of course, with very little change, this can be used to convert a BMP to JPG:


 TJPEGImage * jpeg = new TJPEGImage();
 Graphics::TBitmap * bmp = new Graphics::TBitmap();
 bmp->LoadFromFile("c:\\testdata\\sample.bmp");
 jpeg->Assign(bmp);
 jpeg->CompressionQuality = 50;
 jpeg->SaveToFile("c:\\testdata\\sample.jpg");
 delete bmp;
 delete jpeg;