📄 dib.cpp
字号:
//
hMemDc = CreateCompatibleDC(hDc);
// Create a DDB bitmap
//
nWidth = 320;
nHeight = 240;
hBitmap = CreateCompatibleBitmap(hDc, nWidth, nHeight);
hTmpBmp = CreateCompatibleBitmap(hDc, nWidth, nHeight);
SelectObject(hMemDc, hBitmap);
if(!BitBlt(hMemDc, 0, 0, nWidth, nHeight, hDc, 0, 0, SRCCOPY)){
MessageBox(NULL, "BitBlt error", "error", MB_OK);
return 0;
}
BITMAP bm;
GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bm);
hBitmap = (HBITMAP)SelectObject(hMemDc, hTmpBmp);
// Initiate the BitmapInfo structure...
//
ZeroMemory(&bmi, sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
dwDIBSize = lpBI->biSize + sizeof(RGBQUAD);
DWORD dwOffset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD);
// Write the DIB file...
if(!GetDIBits(hDc, hBitmap, 0, bm.bmHeight, NULL, &bmi, DIB_RGB_COLORS))
MessageBox(NULL, "GetDIBits error", "error", MB_OK);
if(bmi.bmiHeader.biSizeImage == 0)
bmi.bmiHeader.biSizeImage = WIDTHBYTES((bm.bmWidth) * (bm.bmBitsPixel)) * bm.bmHeight;
dwDIBSize += bmi.bmiHeader.biSizeImage;
// Initlize BitmapFileHeader structure...
//
bmfHdr.bfType = 0x4d42; // BM
bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + lpBI->biSize + sizeof(RGBQUAD);
LPVOID pointer = pbFile;
memcpy(pbFile, &bmfHdr, sizeof(BITMAPFILEHEADER));
pointer = (LPVOID) ((DWORD)pointer + sizeof(BITMAPFILEHEADER));
memcpy(pointer, &bmi, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD));
pointer = (LPVOID) ((DWORD)pointer + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD));
if(GetDIBits(hMemDc, hBitmap, 0, (UINT)bm.bmHeight, pointer, &bmi, DIB_RGB_COLORS) == 0)
MessageBox(NULL, "GetDIBits error", "error", MB_OK);
DeleteDC(hMemDc);
DeleteObject(hTmpBmp);
DeleteObject(hBitmap);
return (PVOID)pbFile;
}
bool CDib::SaveDIBintoDatebase(HDC hDc, RECT rect, float fBlackness, int nObjectID, int nCameraID)
// Function: To save the part of image displayed in hDc designated by rect
// in the database, together with the value of fBlackness, nObjectID,
// and nCameraID. The image is saved in the form of a complete DIB file.
// This procedure is generally used when an illegal emission is found, and
// the state is to be stored.
// Return Value: If succeed return 1,
// otherwise 0.
{
int nWidth, nHeight;
char lpszMessage[100];
HBITMAP hTmpBmp;
DWORD dwDIBSize;
LPBITMAPINFOHEADER lpBI = (LPBITMAPINFOHEADER)&bmi;
pbFile = pbImageBuffer;
if (!pbFile){
sprintf(lpszMessage, "Memory allocating error");
MessageBox(NULL, lpszMessage, "error", MB_OK);
return 0;
}
// Create memory DC
//
hMemDc = CreateCompatibleDC(hDc);
// Create a DDB bitmap
//
nWidth = 320; //640;
nHeight = 240; //480;
hBitmap = CreateCompatibleBitmap(hDc, nWidth, nHeight);
hTmpBmp = CreateCompatibleBitmap(hDc, nWidth, nHeight);
SelectObject(hMemDc, hBitmap);
if(!BitBlt(hMemDc, 0, 0, nWidth, nHeight, hDc, 0, 0, SRCCOPY))
MessageBox(NULL, "BitBlt error", "error", MB_OK);
BITMAP bm;
GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bm);
hBitmap = (HBITMAP)SelectObject(hMemDc, hTmpBmp);
// Initiate the BitmapInfo structure...
//
ZeroMemory(&bmi, sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
dwDIBSize = lpBI->biSize + sizeof(RGBQUAD);
DWORD dwOffset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD);
// Write the DIB file...
if(!GetDIBits(hDc, hBitmap, 0, bm.bmHeight, NULL, &bmi, DIB_RGB_COLORS))
MessageBox(NULL, "GetDIBits error", "error", MB_OK);
if(bmi.bmiHeader.biSizeImage == 0)
bmi.bmiHeader.biSizeImage = WIDTHBYTES((bm.bmWidth) * (bm.bmBitsPixel)) * bm.bmHeight;
dwDIBSize += bmi.bmiHeader.biSizeImage;
// Initlize BitmapFileHeader structure...
//
bmfHdr.bfType = 0x4d42; // BM
bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + lpBI->biSize + sizeof(RGBQUAD);
LPVOID pointer = pbFile;
memcpy(pbFile, &bmfHdr, sizeof(BITMAPFILEHEADER));
pointer = (LPVOID) ((DWORD)pointer + sizeof(BITMAPFILEHEADER));
memcpy(pointer, &bmi, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD));
pointer = (LPVOID) ((DWORD)pointer + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD));
if(GetDIBits(hMemDc, hBitmap, 0, (UINT)bm.bmHeight, pointer, &bmi, DIB_RGB_COLORS) == 0)
MessageBox(NULL, "GetDIBits error", "error", MB_OK);
DeleteDC(hMemDc);
DeleteObject(hTmpBmp);
DeleteObject(hBitmap);
return WriteResult((DBREAL) fBlackness, (LPSTR)pbFile, dwDIBSize, nObjectID, nCameraID);
}
bool CDib::LoadDIB(LPSTR lpstrFileName)
// Function: Load a bmp file designated by lpstrFileName.
// After this operation,
// the pbFile point to the data block contains the entire bmp file;
// pBmfHdr point to the BitMapFileHeader struct;
// pBmiHdr points to BitMapInfoHeader structure;
// pvBits points to the real pixel bits.
// After this operation, bBmpValid is set true.
{
DWORD dwErrorCode;
char lpszMessage[100];
hFile = CreateFile(lpstrFileName, GENERIC_READ, // | GENERIC_WRITE,
0, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE){
// File open failed
MessageBox(NULL, "File could not be opened.", "error", MB_OK);
return 0;
}
// Create file mapping
//
DWORD dwNumberOfBytesToMap = si.dwAllocationGranularity * 10;
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, 0); //1000000, NULL);//PAGE_READWRITE, 0, 1000000, NULL);
if(hFileMapping == NULL){
sprintf(lpszMessage, "File mapping failed. Error No.%d", GetLastError());
MessageBox(NULL, lpszMessage, "error", MB_OK);
CloseHandle(hFile);
return 0;
}
// Mapping view...
//
pbFile = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);//FILE_MAP_WRITE, 0, 0, 0);//dwNumberOfBytesToMap);
if (!pbFile){
dwErrorCode = GetLastError();
sprintf(lpszMessage, "Calling MapViewOfFile error, \n error code %d", dwErrorCode);
MessageBox(NULL, lpszMessage, "error", MB_OK);
return 0;
}
pBmfHdr = (PBITMAPFILEHEADER)pbFile;
pBmiHdr = (PBITMAPINFOHEADER)((DWORD)pbFile + sizeof(BITMAPFILEHEADER));
pvBits = (PVOID)((DWORD)pbFile + pBmfHdr->bfOffBits);
bBmpValid = 1;
return 1;
}
bool CDib::LoadDIBFromMemory(LPSTR lpstrBits)
// Function: Fill the corresponding domain of this object by the "DIB file in memory"
// After this operation,
// the pbFile point to the data block contains the entire bmp file;
// pBmfHdr point to the BitMapFileHeader struct;
// pBmiHdr points to BitMapInfoHeader structure;
// pvBits points to the real pixel bits.
// After this operation, bBmpValid is set true.
// Remark: The memory block pointed by lpstrBits can not be released until
// all operation on this dib completes.
// Caution: Never try to call CloseDIB() to clear this object if it is load by this way.
{
DWORD dwErrorCode;
char lpszMessage[100];
pbFile = (unsigned char*)lpstrBits;
if (!pbFile){
dwErrorCode = GetLastError();
sprintf(lpszMessage, "Invalidate Date", dwErrorCode);
MessageBox(NULL, lpszMessage, "error", MB_OK);
return 0;
}
pBmfHdr = (PBITMAPFILEHEADER)pbFile;
pBmiHdr = (PBITMAPINFOHEADER)((DWORD)pbFile + sizeof(BITMAPFILEHEADER));
pvBits = (PVOID)((DWORD)pbFile + pBmfHdr->bfOffBits);
bBmpValid = 1;
return 1;
}
bool CDib::CloseDIBFile()
{
bBmpValid = 0;
UnmapViewOfFile(pbFile);
CloseHandle(hFileMapping);
CloseHandle(hFile);
pbFile = NULL;
pBmfHdr = NULL;
pBmiHdr = NULL;
pvBits = NULL;
return 1;
}
bool CDib::InvalidateDIB()
{
bBmpValid = 0;
pbFile = NULL;
pBmfHdr = NULL;
pBmiHdr = NULL;
pvBits = NULL;
return 1;
}
bool CDib::ShowDIB(HDC hDc, RECT rect)
// Function: Show image of the DIB file contained in this object in the rectangle of hDc.
// The image may be exagrated or shrinked to fit the rectangle.
{
//if(!SetDIBitsToDevice(hDc, xDest, yDest, pBmiHdr->biWidth, pBmiHdr->biHeight,
// 0, 0, 0, pBmiHdr->biHeight, (PVOID)pvBits, (PBITMAPINFO)pBmiHdr, DIB_RGB_COLORS))
if(StretchDIBits(hDc, rect.left, rect.top, rect.right - rect.left,
rect.bottom - rect.top, 0, 0, pBmiHdr->biWidth, pBmiHdr->biHeight,
(PVOID)pvBits, (PBITMAPINFO)pBmiHdr, DIB_RGB_COLORS, SRCCOPY) == GDI_ERROR)
MessageBox(NULL, "Show bmp error", "error", MB_OK);
return 1;
}
CDib::~CDib()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -