implementation save bitmap and load bitmap directly. fast and ease on mfc class
void CMRSMATH1Dlg::Loadit(TCHAR *destination, CDC &memdc)
{
CImage img;
PBITMAPINFO bmi;
BITMAPINFOHEADER Info;
BITMAPFILEHEADER bFileHeader;
CBitmap bm;
CFile file2;
file2.Open(destination, CFile::modeRead | CFile::typeBinary);
file2.Read(&bFileHeader, sizeof(BITMAPFILEHEADER));
file2.Read(&Info, sizeof(BITMAPINFOHEADER));
BYTE ch;
int width = Info.biWidth;
int height = Info.biHeight;
if (height < 0)height = -height;
int size1 = width*height * 3;
int size2 = ((width * 24 + 31) / 32) * 4 * height;
int widthnew = (size2 - size1) / height;
BYTE * buffer = (BYTE *)GlobalAlloc(GPTR, size2);
//////////////////////////
HGDIOBJ old;
unsigned char alpha = 0;
int z = 0;
z = 0;
int gap = (size2 - size1) / height;
for (int y = 0;y < height;y++)
{
for (int x = 0;x < width*3;x++)
{
file2.Read(&ch, 1);
buffer[z] = ch;
z++;
}
for (int z1 = 0;z1 <gap;z1++)
{
file2.Read(&ch,1);
}
}
bm.CreateCompatibleBitmap(&memdc, width, height);
bm.SetBitmapBits(size1,buffer);
old = memdc.SelectObject(&bm);
///////////////////////////
//bm.SetBitmapBits(size1, buffer);
GetDC()->BitBlt(1, 95, width, height, &memdc, 0, 0, SRCCOPY);
memdc.SelectObject(&old);
bm.DeleteObject();
GlobalFree(buffer);
file2.Close();
}
void CMRSMATH1Dlg::saveit(CBitmap &bit1, CDC &memdc, TCHAR *destination)
{
BITMAP bm;
PBITMAPINFO bmi;
BITMAPINFOHEADER Info;
BITMAPFILEHEADER bFileHeader;
CFile file1;
CSize size = bit1.GetBitmap(&bm);
int z = 0;
BYTE ch = 0;
size.cx = bm.bmWidth;
size.cy = bm.bmHeight;
int width = size.cx;
int size1 = (size.cx)*(size.cy);
int size2 = size1 * 3;
size1 = ((size.cx * 24 + 31) / 32) *4* size.cy;
BYTE * buffer = (BYTE *)GlobalAlloc(GPTR, size2);
bFileHeader.bfType = 'B' + ('M' << 8);
bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bFileHeader.bfSize = bFileHeader.bfOffBits + size1;
bFileHeader.bfReserved1 = 0;
bFileHeader.bfReserved2 = 0;
Info.biSize = sizeof(BITMAPINFOHEADER);
Info.biPlanes = 1;
Info.biBitCount = 24;//bm.bmBitsPixel;//bitsperpixel///////////////////32
Info.biCompression = BI_RGB;
Info.biWidth =bm.bmWidth;
Info.biHeight =-bm.bmHeight;///reverse pic if negative height
Info.biSizeImage =size1;
Info.biClrImportant = 0;
if (bm.bmBitsPixel <= 8)
{
Info.biClrUsed = 1 << bm.bmBitsPixel;
}else
Info.biClrUsed = 0;
Info.biXPelsPerMeter = 0;
Info.biYPelsPerMeter = 0;
bit1.GetBitmapBits(size2, buffer);
file1.Open(destination, CFile::modeCreate | CFile::modeWrite |CFile::typeBinary,0);
file1.Write(&bFileHeader, sizeof(BITMAPFILEHEADER));
file1.Write(&Info, sizeof(BITMAPINFOHEADER));
unsigned char alpha = 0;
for (int y = 0;y<size.cy;y++)
{
for (int x = 0;x<size.cx;x++)
{
//for reverse picture below
//z = (((size.cy - 1 - y)*size.cx) + (x)) * 3;
z = (((y)*size.cx) + (x)) * 3;
file1.Write(&buffer[z], 1);
file1.Write(&buffer[z + 1], 1);
file1.Write(&buffer[z + 2], 1);
}
for (int z = 0;z < (size1 - size2) / size.cy;z++)
{
file1.Write(&alpha, 1);
}
}
GlobalFree(buffer);
file1.Close();
file1.m_hFile = NULL;
}