我有个网络应用程序,用IE打开的,但要为程序加入一个背景图,如果是bmp的就会大大加大程序,这样会使IE打开程序变慢很多,如何在加入图片程序不会加的很大,当然听说用JPG图片,程序就会太大,但在MFC用怎么用到JPG图片呢,谢谢大家。

解决方案 »

  1.   

    通常减 少程序的大小的办法有:1. 用 #pragma optimize 打开编译开关 /Oy (Only apply to Enterprise version VC compiler as MSDN said)
    e.g, 
    #pragma optiomize ("y", on)
    这将去掉框架指示器,减少程序大小(不太明显)2. 链接指示
    #pragma comment(linker,"/merge:.text=.data")
    合并文本和数据段上面方法是安全的, 下面方法依照你的程序使用情况:3. 不支持win98模式
    #pragma comment(linker,"/opt:nowin98")4. 下面方法不推荐使用,仅在vxd或驱动中用(指示段对齐通常4k,减 少至16)
    #pragma comment(linker, "/align:16")
      

  2.   

    另外,如果你用MFC sharing dll, 上面方法效果明显.
      

  3.   

    #define HIMETRIC_INCH 2540
    // This function loads a file into an IStream.
    BOOL LoadBitmapFromRes(HINSTANCE hInst, int resID, CBitmap& bitmap)
    {
    // Make resource ident string e.g. "#307"
    LPPICTURE pIPicture = NULL;
    LPPICTURE* ppIPicture = &pIPicture; // Load resource
    HRSRC hRes  = FindResource(hInst, MAKEINTRESOURCE(resID), _T("PICTURE"));
    DWORD dwResSize = SizeofResource(hInst, hRes);
    HGLOBAL hResGlobal  = LoadResource(hInst, hRes);
    LPVOID lpResMem  = LockResource(hResGlobal); // The
    // Make a new global memory block with GMEM_MOVEABLE
    HGLOBAL hNewMem = GlobalAlloc(GMEM_MOVEABLE, dwResSize);
    LPVOID lpNewMem = GlobalLock(hNewMem); // Copy old to new
    memcpy(lpNewMem, lpResMem, dwResSize); // Unlock both memory's
    GlobalUnlock(hNewMem);
    GlobalUnlock(hResGlobal); // Pass new memory to CreateStreamOnHGlobal and create IStream* from global memory
    // The memory must be MOVEABLE for this to work. I tried making the return
    // from LoadResource() MOVEABLE with GlobalReAlloc() but it would not work.
    LPSTREAM pstm = NULL;
    HRESULT hr = CreateStreamOnHGlobal(hNewMem, TRUE, &pstm); // Destroy last picture if exists
    if (*ppIPicture)
    (*ppIPicture)->Release(); // Create IPicture from image file
    hr = OleLoadPicture(pstm, dwResSize, FALSE, IID_IPicture, (LPVOID*)ppIPicture);
    pstm->Release(); BOOL bSuccess = FALSE;
    if (hr == S_OK)
    {
    long hmWidth, hmHeight;
    pIPicture->get_Width(&hmWidth);
    pIPicture->get_Height(&hmHeight); HDC hdc = ::GetDC(NULL);
    CDC* pDC = CDC::FromHandle(hdc);
    CDC memdc;
    memdc.CreateCompatibleDC(pDC); // convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(memdc.m_hDC, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(memdc.m_hDC, LOGPIXELSY), HIMETRIC_INCH); if (bitmap.m_hObject != NULL)
    bitmap.DeleteObject(); bitmap.CreateCompatibleBitmap(pDC, nWidth, nHeight);
    CBitmap* pOldBitmap = memdc.SelectObject(&bitmap); pIPicture->Render(memdc.m_hDC, 0, 0, nWidth, nHeight, 0, hmHeight,
         hmWidth, -hmHeight, &CRect(0,0,nWidth,nHeight)); pIPicture->Release();
    memdc.SelectObject(pOldBitmap);
    memdc.DeleteDC();
    ::ReleaseDC(NULL, hdc); bSuccess = TRUE;
    } return bSuccess;
    }