GDI+ 在窗口显示内存图片

bool CTest::ShowImage(CWnd *pWnd, PBYTE pbImgData, int iImgDataLen)
{
    if (pWnd == NULL || pbImgData == NULL || iImgDataLen < 1)
    {
        return false;
    }

    // 创建流
    IStream *pstmp = NULL;
    CreateStreamOnHGlobal(NULL, TRUE, &pstmp);
    if (pstmp == NULL)
    {
        return  false;
    }

    // 初始化流
    LARGE_INTEGER LiTemp = {0};
    ULARGE_INTEGER uLiZero = {0};
    ULONG ulRealSize = 0;
    pstmp->Seek(LiTemp, STREAM_SEEK_SET,NULL);
    pstmp->SetSize(uLiZero);

    // 将图像放入流中
    pstmp->Write(pbImgData, iImgDataLen, &ulRealSize);

    // 从流创建位图
    Bitmap bmpSrc(pstmp);

    // 获取显示图片窗口的宽度和高度
    RECT rtWnd;
    pWnd->GetWindowRect(&rtWnd);
    int iWndWidth=rtWnd.right-rtWnd.left;
    int iWndHeight=rtWnd.bottom-rtWnd.top;

    CDC *pwndDC = NULL;
    pwndDC = pWnd->GetDC();
    if (pwndDC != NULL)
    {
        Graphics grDraw(pwndDC->m_hDC);
        RectF rtDst((REAL)0, (REAL)0, (REAL)iWndWidth, (REAL)iWndHeight);
        grDraw.DrawImage(&bmpSrc, rtDst, (REAL)0, (REAL)0, (REAL)bmpSrc.GetWidth(), (REAL)bmpSrc.GetHeight(), UnitPixel);
        pWnd->ReleaseDC(pwndDC);
    }

    pstmp->Release();
    pstmp = NULL;

    return true;
}


调用例子:

ShowImage(GetDlgItem(IDC_STATIC__IMG),  pbImg,  iImgLen);

 

 

 

 

 

 

 

你可能感兴趣的:(窗口,内存,mfc,GDI+,显示图片)