Member 13855312
void CMRSMATHDlg::Loadit(TCHAR *destination)
{
CImage img;
bool reverse=false;
BITMAPINFOHEADER Info;
BITMAPFILEHEADER bFileHeader;
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 (load1.m_hObject!=NULL)
{
load1.Detach();
//load1.DeleteObject();
//load1.CreateCompatibleBitmap(GetDC(),width,height);
}
if (height > 0)
{
reverse = true;
}
if (height<0)
height = -height;
int size1 = width*height * 3;
int size2 = ((width * 24 + 31) / 32) * 4 * height;
int gap = (size2 - size1) / height;
BYTE * buffer1 = (BYTE *)GlobalAlloc(GPTR, size2);
//////////////////////////
HGDIOBJ old;
unsigned char alpha = 0;
int z = 0;
z = 0;
CRect rc;
GetClientRect(&rc);
memset(buffer1,255,size2);
file2.Read(buffer1,size2);
int dpi=GetDC()->GetDeviceCaps(LOGPIXELSX);
int dpmm=dpi/25; //dots per mm<br/>
BITMAPINFO *info1;
info1=(BITMAPINFO *)new BYTE[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256];
info1->bmiHeader=Info;
info1->bmiHeader.biXPelsPerMeter=dpmm*1000;
info1->bmiHeader.biYPelsPerMeter=dpmm*1000;
info1->bmiHeader.biBitCount=24;
RGBQUAD *rgbPalette=(RGBQUAD *)malloc(sizeof(RGBQUAD)*256);
for(int i=0; i<256; i++){
rgbPalette[i].rgbRed = (byte)i;
rgbPalette[i].rgbGreen = (byte)i;
rgbPalette[i].rgbBlue = (byte)
rgbPalette[i].rgbReserved = 0;}
memcpy(info1->bmiColors, rgbPalette, sizeof(RGBQUAD) * 256);
//info1.bmiColors=
CDC mem;
mem.CreateCompatibleDC(0);
HDC hDC = mem.m_hDC;
HDC m_hDC = CreateCompatibleDC(hDC);
load1.Attach(CreateDIBSection(NULL,info1,DIB_RGB_COLORS, (LPVOID*)0,0,0));
load1.SetBitmapBits(size2,buffer1);
load1width=width;
load1height=height;
CImage s1;
GlobalFree(buffer1);
file2.Close();
}
void CMRSMATHDlg::saveit(CBitmap &bit1, CDC &memdc, TCHAR *destination,bool isreverse)
{
BITMAP bm;
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 size1 = (size.cx)*(size.cy);
int size2 = size1 * 3;
size1 = (((size.cx) * 24 + 31) / 32) *4* (size.cy);
int gap = (size1 - size2) / (size.cy);
BYTE * buffer = (BYTE *)GlobalAlloc(GPTR, size2);
BYTE * buffer1 = (BYTE *)GlobalAlloc(GPTR, size.cx*3);
BYTE * gbuffer = (BYTE *)GlobalAlloc(GPTR, gap);
memset(gbuffer, 0, gap);
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 =size.cx;
Info.biHeight =size.cy;///reverse pic if negative height
Info.biSizeImage =size1;
Info.biClrImportant = 0;
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;
int i;
for (int y = 0;y<size.cy;y++)
{
i=0;
for (int x = 0;x<size.cx;x++)
{
//for reverse picture below
//z = (((size.cy - 1 - y)*size.cx) + (x)) * 3;
if (isreverse)
z = (((size.cy-1-y)*(size.cx)) + (x)) * 3;
else
z=(y*size.cx+x)*3;
buffer1[i]=buffer[z];
buffer1[i+1]=buffer[z+1];
buffer1[i+2]=buffer[z+2];
i+=3;
}
file1.Write(buffer1,size.cx*3);
file1.Write(&gbuffer, gap);
}
GlobalFree(buffer);
GlobalFree(gbuffer);
GlobalFree(buffer1);
file1.Close();
file1.m_hFile = NULL;
}