how to use wxMetaFile(wxMetaFileDC) to convert WMF image to jpg file

114 views
Skip to first unread message

jank...@sina.com

unread,
Sep 17, 2012, 9:46:19 PM9/17/12
to wx-u...@googlegroups.com
                      In my program, I need convert some WMF format images which are dynamically generated to save jpg format files,  how can I use wxMetaFile(wxMetaFileDC) to convert? could anybody give me a piece of code?

Gadget/Steve

unread,
Sep 18, 2012, 2:09:46 AM9/18/12
to wx-u...@googlegroups.com
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users
As the help file says that wxMetaFile can only be used to copy data to
the clipboard for the moment the simple answer is that you can't. If
you were working in wxPython the simple answer would be to use PIL, the
python image library, if you are in C++ you will have to find another
library that supports this.

Gadget/Steve

Julian Smart

unread,
Sep 18, 2012, 5:29:08 PM9/18/12
to wx-u...@googlegroups.com
On Windows, you could do something like the following.

Regards,

Julian

static void wxHIMETRICToPixel(LONG *x, LONG *y)
{
*x = (*x) * 96 / 2540;
*y = (*y) * 96 / 2540;
}

// This function is more accurate and consistent than the wxWidgets one
static wxSize wxEnhMetaFileGetSize(wxEnhMetaFile& mf)
{
wxSize size = wxDefaultSize;

if ( mf.Ok() )
{
ENHMETAHEADER hdr;
if (
!::GetEnhMetaFileHeader(((HENHMETAFILE)mf.GetHENHMETAFILE()),
sizeof(hdr), &hdr) )
{
wxLogLastError(_T("GetEnhMetaFileHeader"));
}
else
{
// the width and height are in HIMETRIC (0.01mm) units,
transform
// them to pixels
LONG w = hdr.rclFrame.right,
h = hdr.rclFrame.bottom;

wxHIMETRICToPixel(&w, &h);

size.x = w;
size.y = h;
}
}
return size;
}
#endif

// Image is simply read in into hash map keyed by its name
if (FileUtilities::GetExtension(name).Lower() == wxT("wmf") ||
FileUtilities::GetExtension(name).Lower() == wxT("emf"))
{
#ifdef __WXMSW__
if (wxFileExists(filename))
{
wxLogNull log;
wxEnhMetaFile* enhMetafile = new wxEnhMetaFile(filename);
if (enhMetafile->IsOk())
{
//wxSize sz = enhMetafile->GetSize();
wxSize sz = wxEnhMetaFileGetSize(*enhMetafile);

if (sz.x != 0 && sz.y != 0)
{
wxRect rect(0, 0, sz.x, sz.y);

wxBitmap bitmap(sz.x, sz.y);
wxMemoryDC dc;
dc.SelectObject(bitmap);
dc.SetBackground(*wxWHITE_BRUSH);
dc.Clear();

if (enhMetafile->Play(& dc, & rect))
{
dc.SelectObject(wxNullBitmap);

if (bitmap.IsOk())
{
wxImage image = bitmap.ConvertToImage();

...
delete enhMetafile;
}
}
}
}
delete enhMetafile;
}


jank...@sina.com

unread,
Sep 20, 2012, 6:35:54 AM9/20/12
to wx-u...@googlegroups.com
Thank you very much, I'll test it later

jank...@sina.com

unread,
Sep 20, 2012, 7:58:44 AM9/20/12
to wx-u...@googlegroups.com


On Wednesday, September 19, 2012 5:29:12 AM UTC+8, Julian wrote:
Hi, Julia,
 I have tested it with a image which saved from my program and have been opened properly by ms paint, the code does not work, and then I debuged my program and found it at the position if (enhMetafile->IsOk()), it always fail. what does I do not check ?

Julian Smart

unread,
Sep 20, 2012, 10:53:45 AM9/20/12
to wx-u...@googlegroups.com
On 20/09/2012 12:58, jank...@sina.com wrote:
>
> Hi, Julia,
> I have tested it with a image which saved from my program and have
> been opened properly by ms paint, the code does not work, and then I
> debuged my program and found it at the position if
> (enhMetafile->IsOk()), it always fail. what does I do not check ?
I guess it's not a supported type of enhanced metafile but alas I don't
know any more.. .sorry!

Regards,

Julian
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users


--
Julian Smart, Anthemion Software Ltd.
www.anthemion.co.uk | +44 (0)131 229 5306
Tools for writers: www.writerscafe.co.uk
Ebook creation: www.jutoh.com
wxWidgets RAD: www.dialogblocks.com

Reply all
Reply to author
Forward
0 new messages