0

我正在构建一个显示图像的 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;
}
4

1 回答 1

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;
于 2017-10-31T15:38:12.807 回答