1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
//write HBITMAP:
friend std::ostream& operator << (std::ostream& lhs, BitmapDC &hBitmap)
{
//get hbitmap size:
BITMAP bm;
GetObject(hBitmap.bitmapcurrent, sizeof(BITMAP), &bm);
LONG lWidth=bm.bmWidth;
LONG lHeight=bm.bmHeight;
BYTE* pBitmapBits;
BitBlt(GetWindowDC(ActivatedForm), 100, 100, bm.bmWidth, bm.bmHeight, hBitmap.hdcbitmap, 0, 0, SRCCOPY);
WORD wBitsPerPixel=bm.bmBitsPixel;
unsigned long pixel_data_size = lHeight * ( ( lWidth * ( wBitsPerPixel / 8 ) ) + 0 );
// Some basic bitmap parameters
unsigned long headers_size = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
BITMAPINFOHEADER bmpInfoHeader = {0};
// Set the size
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
// Bit count
bmpInfoHeader.biBitCount = wBitsPerPixel;
// Use all colors
bmpInfoHeader.biClrImportant = 0;
// Use as many colors according to bits per pixel
bmpInfoHeader.biClrUsed = 0;
// Store as un Compressed
bmpInfoHeader.biCompression = BI_RGB;
// Set the height in pixels
bmpInfoHeader.biHeight = lHeight;
// Width of the Image in pixels
bmpInfoHeader.biWidth = lWidth;
// Default number of planes
bmpInfoHeader.biPlanes = 1;
// Calculate the image size in bytes
bmpInfoHeader.biSizeImage = pixel_data_size;
//getting the HBitmap pixel data from HDC:
pBitmapBits=new BYTE[bmpInfoHeader.biSizeImage];
GetDIBits(hBitmap.hdcbitmap , hBitmap.bitmapcurrent, 0, bm.bmHeight, pBitmapBits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);
BITMAPFILEHEADER bfh = {0};
// This value should be values of BM letters i.e 0x4D42
// 0x4D = M 0×42 = B storing in reverse order to match with endian
bfh.bfType = 0x4D42;
//bfh.bfType = 'B'+('M' << 8);
// <<8 used to shift ‘M’ to end */
// Offset to the RGBQUAD
bfh.bfOffBits = headers_size;
// Total size of image including size of headers
bfh.bfSize = headers_size + bmpInfoHeader.biSizeImage ;
// Write the File header:
lhs.write((char*)&bfh, sizeof(bfh));
//Write the bitmap info header:
lhs.write((char*)&bmpInfoHeader,sizeof(bmpInfoHeader));
//write pixel data:
lhs.write((char*)pBitmapBits, bmpInfoHeader.biSizeImage);
delete []pBitmapBits;
return lhs;
}
//read HBITMAP:
friend std::istream& operator >> (std::istream& lhs, BitmapDC &hBitmap)
{
MessageBox("read it");
BITMAPFILEHEADER bfh = {0};
lhs.read((char*)&bfh, sizeof(bfh));
BITMAPINFOHEADER bmpInfoHeader = {0};
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
lhs.read((char*)&bmpInfoHeader,sizeof(bmpInfoHeader));
BYTE* pBitmapBits=new BYTE[bmpInfoHeader.biSizeImage];
lhs.read((char*)pBitmapBits, bmpInfoHeader.biSizeImage);
hBitmap.init(bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
SetDIBitsToDevice(hBitmap.hdcbitmap, 0, 0, bmpInfoHeader.biWidth, bmpInfoHeader.biHeight, 0, 0, 0, bmpInfoHeader.biHeight, pBitmapBits,(BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);
BitBlt(GetWindowDC(ActivatedForm), 200, 100, bmpInfoHeader.biWidth, bmpInfoHeader.biHeight, hBitmap.hdcbitmap, 0, 0, SRCCOPY);
delete []pBitmapBits;
return lhs;
}
void save(string FileName)
{
ofstream WriteOnFile(FileName.c_str(), ios::out | ios::binary);
WriteOnFile << bitmapcurrent;
WriteOnFile.close();
ifstream ReadOnFile(FileName.c_str(), ios::in | ios::binary);
ReadOnFile >>bitmapcurrent;
ReadOnFile.close();
}
|