Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This function is called by my virtual print driver to convert bmp to EMF.

C++
void GetEmfByBytes(BYTE * pBits, int iWidth, int iHeight, WCHAR* szPrinterName, WCHAR* szDestinationFile,DWORD rop=SRCCOPY)
{
    BITMAP bm;

    HDC hdcMem = CreateCompatibleDC(GetDC(NULL));
    HDC hPrinterDC = ::CreateDC(NULL,szPrinterName, NULL, NULL);

    BITMAPINFOHEADER bmih;
    memset(&bmih, 0, sizeof(BITMAPINFOHEADER));

    bmih.biSize = sizeof(BITMAPINFOHEADER);
    bmih.biBitCount = 1;
    bmih.biCompression = BI_RGB;
    bmih.biPlanes = 1;
    bmih.biWidth = iWidth;
    bmih.biHeight = iHeight;

    BITMAPINFO bmi;
    memset(&bmi, 0, sizeof(BITMAPINFO));

    bmi.bmiHeader = bmih;

    HBITMAP hBmp = ::CreateDIBitmap(hPrinterDC,&bmih,CBM_INIT,pBits,&bmi,0);

    GetObject(hBmp, sizeof(BITMAP), &bm );
    SelectObject(hdcMem, hBmp);
    HDC emfdc = CreateEnhMetaFile(hPrinterDC, szDestinationFile, NULL, L"");

    if(emfdc!=NULL){
        StretchBlt(emfdc,0, 0, bm.bmWidth, -bm.bmHeight, hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, rop);// From task     manager I found the memory of spoolsv.exe increased 
        // 4M in xp, while 8M in windows 2003.
        //DeleteDC(emfdc);
        HENHMETAFILE hEMFFile = CloseEnhMetaFile(emfdc);// 4M memory were released in both xp and windows 2003. So     4M memory were still not released in windows 2003. Memory Leak!
        DeleteEnhMetaFile(hEMFFile);
        //DeleteDC(emfdc);
    }else{
        logOut( wstring(L"can't create emf")+szDestinationFile );
    }

    DeleteDC(hPrinterDC);
    DeleteDC(hdcMem);
    DeleteObject(hBmp);
}



anyone knows why? thanks a lot! Please~!
Posted
Updated 13-Oct-11 5:41am
v3

Perhaps this article[^] can help you determine the cause.
 
Share this answer
 
Today I find when I just install my virtual printer once in windows 2003, the memory become the same as xp. But when I install more than two same virtual printer in windows 2003, StretchBlt allocated two times memory in windows 2003 than xp.
 
Share this answer
 
I finally find the problem. The srcDc and desDc of StretchBlt must be compatible. So
HDC hdcMem = CreateCompatibleDC(GetDC(NULL));
HDC hPrinterDC = ::CreateDC(NULL,szPrinterName, NULL, NULL);
should be changed as following:
HDC hPrinterDC = ::CreateDC(NULL,szPrinterName, NULL, NULL);
HDC hdcMem = CreateCompatibleDC(hPrinterDC );
But the problem didn't take place at xp and windows 2000.
 
Share this answer
 
I finally find the problem. The srcDc and desDc of StretchBlt must be compatible. So
C++
HDC hdcMem = CreateCompatibleDC(GetDC(NULL));
HDC hPrinterDC = ::CreateDC(NULL,szPrinterName, NULL, NULL);
should be changed as following:
HDC hPrinterDC = ::CreateDC(NULL,szPrinterName, NULL, NULL);
HDC hdcMem = CreateCompatibleDC(hPrinterDC );

But the problem didn't take place at xp and windows 2000.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900