|
CString StrFilter = "Bitmap files (* .bmp) | * .bmp | All files (*. *) | *. * ||";
CFileDialog Dlg (TRUE, NULL, NULL, NULL, StrFilter, this);
if (! Dlg.DoModal () == IDOK)
return;
CString StrFileName;
StrFileName = Dlg.GetPathName ();
// BITMAPINFO structure pointer
BITMAPINFO * pBmpInfo;
// DIB image data pointer
BYTE * pBmpData;
CFile MyFile;
if (! MyFile.Open (StrFileName, CFile :: modeRead | CFile :: typeBinary))
return;
BITMAPFILEHEADER BmpHeader;
if (MyFile.Read (&BmpHeader, sizeof (BmpHeader))! = sizeof (BmpHeader))
{
AfxMessageBox ("Error reading bitmap file header!");
return;
}
if (BmpHeader.bfType! = 0x4d42)
{
AfxMessageBox ("Not a bitmap file!");
return;
}
BITMAPINFOHEADER BmpInfo;
if (MyFile.Read (&BmpInfo, sizeof (BmpInfo))! = sizeof (BmpInfo))
{
AfxMessageBox ("Error reading bitmap information!");
return;
}
if (BmpInfo.biBitCount! = 24)
{
AfxMessageBox ("Not a true 24-color bitmap, the program does not support it for now!");
return;
}
pBmpInfo = (BITMAPINFO *) new char [sizeof (BITMAPINFOHEADER)];
if (! pBmpInfo)
{
AfxMessageBox ("Memory allocation error!");
return;
}
memcpy (pBmpInfo,&BmpInfo, sizeof (BITMAPINFOHEADER));
DWORD dataBytes = BmpHeader.bfSize-BmpHeader.bfOffBits;
pBmpData = (BYTE *) new char [dataBytes];
if (! pBmpData)
{
AfxMessageBox ("Memory allocation error!");
delete pBmpInfo;
return;
}
if (MyFile.Read (pBmpData, dataBytes)! = dataBytes)
{
AfxMessageBox ("Error reading bitmap data!");
delete pBmpInfo;
delete pBmpData;
return;
}
MyFile.Close ();
CClientDC * pDC = new CClientDC (this);
pDC-> SetStretchBltMode (COLORONCOLOR);
StretchDIBits (pDC-> GetSafeHdc (), 0,0, BmpInfo.biWidth, BmpInfo.biHeight, 0,0,
BmpInfo.biWidth, BmpInfo.biHeight,
pBmpData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY); |
|