使用CreateMetaFile创建WMF文件,并转换为EMF文件

#include <iostream>
#include <stdio.h>
#include <WINDOWS.H>
#include <shellapi.h>
#include <gdiplus.h>
#include <Shlwapi.h>


#pragma comment (lib,"Shlwapi.lib")
#pragma comment(lib,"gdiplus.lib")
#pragma comment(lib, "shell32.lib")

using namespace Gdiplus;

int main(int argc, char **argv)
{
    static HMETAFILE hmf;
    static int       cxClient, cyClient;
    HBRUSH           hBrush;
    HDC              hdcMeta;

    METAFILEPICT mp;
    HDC hDC;
    BYTE* buffer;
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


    hdcMeta = CreateMetaFile("12_.WMF");
    hBrush = CreateSolidBrush(RGB(0, 0, 255));

    Rectangle(hdcMeta, 0, 0, 100, 100);

    MoveToEx(hdcMeta, 0, 0, NULL);
    LineTo(hdcMeta, 100, 100);
    MoveToEx(hdcMeta, 0, 100, NULL);
    LineTo(hdcMeta, 100, 0);

    SelectObject(hdcMeta, hBrush);
    Ellipse(hdcMeta, 20, 20, 80, 80);
    hmf = CloseMetaFile(hdcMeta);
UINT nSize = GetMetaFileBitsEx(hmf, 0, NULL);

buffer
= (BYTE*)malloc(nSize);int err_1 = GetLastError(); UINT bytesCopy = GetMetaFileBitsEx(hmf, nSize, buffer); HENHMETAFILE hEMF = SetWinMetaFileBits(nSize, buffer, NULL, NULL); int err_2 = GetLastError(); HENHMETAFILE newHEMF = CopyEnhMetaFile(hEMF, "new EMF.emf"); DeleteMetaFile(hmf); GdiplusShutdown(gdiplusToken); return 0; }
GetMetaFileBitsEx需要调用两次,第二次调用是为了填充buffer缓冲区
posted @ 2019-11-11 16:22  strive-sun  阅读(789)  评论(0编辑  收藏  举报