When I select two pictures in a row with the 'Browse for Picture' button I get the second picture overlapping the previous picture as the picture shows in my previous post.
Here is the link to the code I am trying to implement:Easy way to display Gif’s, Ico’s, JPEG’s and BMP’s on your window « bits and bytes
I am certain I had this code working as it should before. So there is probably something really obvious I have messed up!
Code:
case IDC_SELECT_PICTURE:
// choose picture.
if (!GetFolderSelection(NULL, pPath2Pic, TEXT("Please select a picture.")))
{
MessageBox(NULL, "You must select a picture.", "Error!", MB_OK);
break;
}
// Setup and display the picture.
{
hFile = CreateFile(Path2Pic, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//DWORD dw = GetLastError();
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
// alloc memory based on file size
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
// read file and store in global memory
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
// Create IPicture from image file
if (gpPicture)
gpPicture->Release();
hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm->Release();
// Retrieve dc and scale picture to fit control.
{
hctrl = GetDlgItem(hdlg, IDC_XSECTION); // set hctrl to picture holder.
GetClientRect(hctrl, &rc); //coordinates of the control client area.
hdcCtrl = GetDC( hctrl );
hmWidth = 0;
hmHeight = 0;
gpPicture->get_Width(&hmWidth);
gpPicture->get_Height(&hmHeight);
WinRatio = (float(rc.right) / float(rc.bottom));
PictRatio = (float(hmWidth) / float(hmHeight));
if(WinRatio > PictRatio)
{
int width;
rect.bottom = rc.bottom;
rect.top = rc.top;
width = (int)(PictRatio * rc.bottom);
diff = (rc.right - width) / 2;
rect.right = width;
rect.left = diff;
}
else
{
int height;
rect.right = rc.right;
rect.left = 0;
height = (int)(float(rc.right) / PictRatio);
diff = int((float(rc.bottom) - float(height)) / 2.0);
rect.bottom = height;
rect.top = diff;
}
}
gpPicture->Render(hdcCtrl, rect.left, rect.top, rect.right, rect.bottom, 0, hmHeight, hmWidth, -hmHeight, &rc);
ReleaseDC( hctrl, hdcCtrl );
gpPicture->Release();
}
// End: Setup and display the picture.
break;
}
break;