Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version
Go to top

EMF to WMF conversion using GDI+

, 20 Jun 2005
A simple program that converts EMF to WMF using GDI+. This project can also be used as a simple kick-off for a console GDI+ program.
#include <afxdisp.h>
#include <gdiplus.h>
#include <stdio.h>

void checkForFiles(char** argv);
wchar_t* convertToUnicode(char* val);

using namespace Gdiplus;

void main(int argc, char** argv)
{
  if (argc < 3)
  {
    fprintf(stderr, "Usage: emf2wmf <input-file> <output-file>\n");
    exit(1);
  }

  checkForFiles(argv);

  GdiplusStartupInput m_gdiplusStartupInput;
  ULONG_PTR m_pGdiToken;
  GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);  //gdi+ init

  // Construct a Metafile object from an existing EMF disk file.
  wchar_t* inFilename = convertToUnicode(argv[1]);
  Metafile* myMetafile = new Metafile(inFilename);

  // Get a handle to the EMF metafile.
  HENHMETAFILE hEmf = myMetafile->GetHENHMETAFILE();

  // Determine the size of the buffer that will receive the converted records.
  UINT size = Metafile::EmfToWmfBits(
    hEmf, 
    0, 
    NULL,
    MM_ANISOTROPIC,
    EmfToWmfBitsFlagsEmbedEmf);

  // Allocate a buffer to receive the converted records.
  BYTE* buffer = new BYTE[size];

  // Convert the EMF records to WMF records.
  INT convertedSize = Metafile::EmfToWmfBits(
    hEmf, 
    size,
    buffer, 
    MM_ANISOTROPIC,
    EmfToWmfBitsFlagsEmbedEmf);

  // Get a handle to the converted metafile.
  HMETAFILE hmf = SetMetaFileBitsEx(size, buffer);

  // Write the WMF metafile to a disk file.
  CopyMetaFile(hmf, argv[2]);

  DeleteMetaFile(hmf);
  DeleteEnhMetaFile(hEmf);
  delete[] buffer;
  delete[] inFilename;

  GdiplusShutdown(m_pGdiToken); //gdi+ end session
}

#include <sys/types.h>
#include <sys/stat.h>

void checkForFiles(char** argv)
{
  struct _stat stat;
  if (_stat(argv[1], &stat) == -1)
  {
    fprintf(stderr, "Input file '%s' does not exist!\n", argv[1]);
    exit(1);
  }

  if (_stat(argv[2], &stat) != -1)
  {
    fprintf(stderr, "Warning.  Output file '%s' already exists.  It will be overwritten.\n", argv[2]);
  }
}

wchar_t* convertToUnicode(char* val)
{
  size_t len = strlen(val)*3;  // make sure we have enough space
  wchar_t* res = (wchar_t*) new char[len];

  if (res == NULL) return res;
  
  MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, val, -1, res, (int) len);
  return res;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Martin Serrano
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 21 Jun 2005
Article Copyright 2005 by Martin Serrano
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid