滚动期间避免重新加载图片

时间:2017-10-31 14:21:58

标签: visual-studio-2008 mfc cbitmap

我正在构建一个显示图像的MFC 2008 mdi应用程序。当我滚动所有图像必须重新加载。我想通过第一次保存CPngImage和CBitmap文件来避免这种情况,然后再重复使用它们。

语句pngBmp [iPic] .GetBitmap(& bi);一旦文件加载,第二次导致中断;

有人可以帮我解决这个问题。代码如下所示;

// Global

#define MAX_IMAGE 100

CPngImage pngImage[MAX_IMAGE];
CBitmap pngBmp[MAX_IMAGE];

    void CFileViewerView::DrawImages(CDC* pDC)
{
    CString pngPath,imageLegend;
    CDC bmDC;
    CBitmap *pOldbmp;
    BITMAP  bi;
    int xPos = marginRect.left, yPos,nPic,nPage;
    int xOffset,yOffset,imageHeight,imageWidth,numPics;
    float aspect;
    BOOL status;

    PayAppReport report = 
            projectList[m_pModeless->GetCurrentProject()].reports[m_pModeless->GetCurrentReport()];

    numPics = report.GetNumberOfPictures();
    bmDC.CreateCompatibleDC(pDC);
    imageHeight = 6500;
    //yPos = pageRect.bottom + marginRect.top;

    nPage = numPics/6;
    for(int iPic=0; iPic<numPics; iPic++)
        {
        if(!imagesLoaded)
            {
            pngPath = report.GetPictures(iPic);
            pngPath.TrimRight();

            pngImage[iPic].LoadFromFile(pngPath);
            status = pngBmp[iPic].Attach(pngImage[iPic].Detach());
            if(!status) 
                {
                CString err;
                err.Format(_T("Cannot open image file \n\r %s"),pngPath);
                MessageBox(err,_T("Image Loading Error"),MB_ICONERROR);
                return;
                }
            }
        else
            pngBmp[iPic].Attach(pngImage[iPic].Detach());

        pngBmp[iPic].GetBitmap(&bi); //   ****** ABORTS Here

        pOldbmp= bmDC.SelectObject(&pngBmp[iPic]);

        aspect = (float)bi.bmWidth / (float)bi.bmHeight;
        imageWidth = (int)(aspect*imageHeight),numPics;

        xOffset = imageWidth;
        yOffset = (int)(-imageHeight*1.20f);

        nPic = iPic;
        if(iPic > 5)
            nPic = iPic - (iPic/6)*6;

        if(nPic == 0)
            {
            xPos = marginRect.left;
            yPos = (pageRect.bottom + marginRect.top)*((iPic/6)+1);
            }
        else if(nPic < 3)
            {
            xPos = marginRect.left;
            yPos += yOffset;
            }
        else if(nPic == 3)
            {
            xPos = marginRect.right-imageWidth;
            yPos = (pageRect.bottom + marginRect.top)*((iPic/6)+1);
            }
        else if(nPic > 3 && nPic < 6)
            {
            xPos = marginRect.right-imageWidth;
            yPos += yOffset;
            }

        _ftprintf(dbgFile,_T("iPic %d nPic %d xPos %d yPos %d\n"),iPic,nPic,xPos,yPos);

        pDC->SetStretchBltMode(HALFTONE);
        SetBrushOrgEx(bmDC,0,0,NULL);
        pDC->StretchBlt(xPos,yPos,imageWidth,-imageHeight,&bmDC,0,0,bi.bmWidth, bi.bmHeight,SRCCOPY);
        bmDC.SelectObject(pOldbmp);
        pngBmp[iPic].Detach();

        imageLegend.Format(_T("Photo %2d"),iPic+1);
        CSize rSize = pDC->GetTextExtent(imageLegend, imageLegend.GetLength());
        pDC->TextOut(xPos+(imageWidth/2)-rSize.cx/2,yPos-imageHeight,imageLegend, imageLegend.GetLength());
        }
    imagesLoaded = true;
}

1 个答案:

答案 0 :(得分:0)

pngBmp[iPic].Attach(pngImage[iPic].Detach());

您已在Detach()上致电pngImage,因此pngImage不再有效。在下一次通话中pngBmp无效。

pngBmp[iPic].GetBitmap(&bi); //   ****** ABORTS Here
如果没有有效的pngBmp[iPic].GetBitmap(&bi);

HBITMAP将失败

更简单的解决方案:

CPngImage源自CBitmap,它可以完成CBitmap所做的一切。您可以通过完全删除pngBmp并使用pngImage进行绘制来简化代码。

您可以使用以下方法测试png图像的有效性:

if(!pngImage[index].m_hObject)
    return;