What is the correct way to cater for loading another image when there is already an image loaded into the CImage object?

162 Views Asked by At

I thought I would try my code in debug mode:

void CSpecialEventVideoconferenceInfoDlg::OnEnChangeBrowseSpecialEventVideoconfPathImage()
{
    CString strImagePath;
    GetDlgItemText(IDC_BROWSE_SPECIAL_EVENT_VIDEOCONF_PATH_IMAGE, strImagePath);
    m_imgPreview.Load(strImagePath);
}

It is very simple and triggers m_imgPreview to load a new image. This variable is of type CImage. I notice in debug mode that when I select a second image that I get an exception raised:

inline void CImage::Attach(
    _In_ HBITMAP hBitmap,
    _In_ DIBOrientation eOrientation) throw()
{
    ATLASSUME( m_hBitmap == NULL );
    ATLASSERT( hBitmap != NULL );

    m_hBitmap = hBitmap;

    UpdateBitmapInfo(eOrientation);
}

It fails on this line:

ATLASSUME( m_hBitmap == NULL );

I have looked at the documentation for this (CImage::Load) and it says nothing about this scenario. What is the correct way to cater for loading another image when there is already an image loaded into the CImage object?

1

There are 1 best solutions below

0
On

At the moment I have adjusted my code like this:

void CSpecialEventVideoconferenceInfoDlg::OnEnChangeBrowseSpecialEventVideoconfPathImage()
{
    CString strImagePath;
    GetDlgItemText(IDC_BROWSE_SPECIAL_EVENT_VIDEOCONF_PATH_IMAGE, strImagePath);

    if (!m_imgPreview.IsNull())
        m_imgPreview.Destroy();

    m_imgPreview.Load(strImagePath);
}

It appears to function still and I get no exceptions when selecting the next image. But if you think there is a better solution then please let me know.