VCproblems
Programmer
Hi!
I am trying to create a bmp file with the following code:
CDibClass::CDibClass()
{
memset(&m_bmpInfoHdr, 0, sizeof(BITMAPINFOHEADER));
m_pBits = NULL;
m_pBMI = NULL;
}
CDibClass::~CDibClass()
{
CloseAll();
}
void CDibClass::CloseAll()
{
if (m_pBMI)
free(m_pBMI);
if (m_pBits)
free(m_pBits);
m_pBits = NULL;
m_pBMI = NULL;
}
void CDibClass:ibInit(int w, int h,LPCTSTR filename)
{
CloseAll();
BITMAPFILEHEADER bfh;
char *ch;
ch =(char*) &bfh.bfType;
*ch='B';
ch++;
*ch='M';
bfh.bfReserved1 =0;
bfh.bfReserved2 = 0;
bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + (w*h); bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
fp = fopen(filename,"wb+"
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,fp);
int i;
m_Width = w;
m_Height = h;
m_bmpInfoHdr.biSize = sizeof(BITMAPINFOHEADER);
m_bmpInfoHdr.biWidth = w;
m_bmpInfoHdr.biHeight = h;
m_bmpInfoHdr.biPlanes = 1;
m_bmpInfoHdr.biBitCount = 8; //24; m_bmpInfoHdr.biCompression =BI_RGB;
m_bmpInfoHdr.biSizeImage = 0 ; //w * h;m_bmpInfoHdr.biClrUsed32; //256;
m_bmpInfoHdr.biClrImportant = ; //256;
m_bmpInfoHdr.biXPelsPerMeter = 0;m_bmpInfoHdr.biYPelsPerMeter = 0;
m_pBMI = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
memcpy(m_pBMI, &m_bmpInfoHdr, sizeof(BITMAPINFOHEADER)); fwrite(&m_bmpInfoHdr,sizeof(BITMAPINFOHEADER),1,fp);
BYTE temp = 0;
BYTE temp1 = 255;
for(i=0; i < 256; i++)
{
m_pBMI->bmiColors.rgbRed = i;
m_pBMI->bmiColors.rgbGreen = i;
m_pBMI->bmiColors.rgbBlue = i;
m_pBMI->bmiColors.rgbReserved = 0;
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&temp,sizeof(BYTE),1,fp);
}
}
void CDibClass::SetBits(HDC hdc,const BYTE* pBits, const int size)
{
if (m_pBits)
free(m_pBits);
m_pBits = (BYTE*)malloc(size);
LPBYTE temp = new BYTE[size];
LPBYTE temp2 = new BYTE[size];
int i;
for(i = 0; i < size; i++)
{
temp = pBits[size - i];
}
for(int line = 0; line < m_Height; line++)
{
for(int row = 0; row < m_Width; row++)
{
temp2[line * m_Width + row] = temp[line * m_Width - row + m_Width];
}
}
for (int j = 0; j < m_Height; j++)
{
memcpy(&m_pBits[j*m_Width], &temp2[(m_Height - 1 - j) * m_Width], m_Width);
fwrite(&m_pBits[j*m_Width],m_Width,1,fp);
}
fclose(fp);
int scanline = SetDIBitsToDevice (hdc, 0, 0, m_Width, m_Height,0, 0, 0, m_Height,temp2, (LPBITMAPINFO) m_pBMI, DIB_RGB_COLORS);
delete [] temp;
delete [] temp2;
}
The code used to call the above functions is as follows:-
g_DibCtl->DibInit(devList.imgSize.width,devList.imgSize.height, filename);
g_DibCtl->SetBits(hdc,g_FPBuffer,devList.imgSize.width*devList.imgSize.height );
The problem that i am facing is : the bitmap is blank, if the bmp file is selected(e.g. in explorer), the preview is not available.If i try to open the bmp file in microsoft photo editor, an error : "Error Reading File" occurs. And if opened in Paint the bmp file is blank.
what is wrong?
Please help me and tell me the possible problem.
Thanks.
I am trying to create a bmp file with the following code:
CDibClass::CDibClass()
{
memset(&m_bmpInfoHdr, 0, sizeof(BITMAPINFOHEADER));
m_pBits = NULL;
m_pBMI = NULL;
}
CDibClass::~CDibClass()
{
CloseAll();
}
void CDibClass::CloseAll()
{
if (m_pBMI)
free(m_pBMI);
if (m_pBits)
free(m_pBits);
m_pBits = NULL;
m_pBMI = NULL;
}
void CDibClass:ibInit(int w, int h,LPCTSTR filename)
{
CloseAll();
BITMAPFILEHEADER bfh;
char *ch;
ch =(char*) &bfh.bfType;
*ch='B';
ch++;
*ch='M';
bfh.bfReserved1 =0;
bfh.bfReserved2 = 0;
bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + (w*h); bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
fp = fopen(filename,"wb+"
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,fp);
int i;
m_Width = w;
m_Height = h;
m_bmpInfoHdr.biSize = sizeof(BITMAPINFOHEADER);
m_bmpInfoHdr.biWidth = w;
m_bmpInfoHdr.biHeight = h;
m_bmpInfoHdr.biPlanes = 1;
m_bmpInfoHdr.biBitCount = 8; //24; m_bmpInfoHdr.biCompression =BI_RGB;
m_bmpInfoHdr.biSizeImage = 0 ; //w * h;m_bmpInfoHdr.biClrUsed32; //256;
m_bmpInfoHdr.biClrImportant = ; //256;
m_bmpInfoHdr.biXPelsPerMeter = 0;m_bmpInfoHdr.biYPelsPerMeter = 0;
m_pBMI = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
memcpy(m_pBMI, &m_bmpInfoHdr, sizeof(BITMAPINFOHEADER)); fwrite(&m_bmpInfoHdr,sizeof(BITMAPINFOHEADER),1,fp);
BYTE temp = 0;
BYTE temp1 = 255;
for(i=0; i < 256; i++)
{
m_pBMI->bmiColors.rgbRed = i;
m_pBMI->bmiColors.rgbGreen = i;
m_pBMI->bmiColors.rgbBlue = i;
m_pBMI->bmiColors.rgbReserved = 0;
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&i,sizeof(BYTE),1,fp);
fwrite(&temp,sizeof(BYTE),1,fp);
}
}
void CDibClass::SetBits(HDC hdc,const BYTE* pBits, const int size)
{
if (m_pBits)
free(m_pBits);
m_pBits = (BYTE*)malloc(size);
LPBYTE temp = new BYTE[size];
LPBYTE temp2 = new BYTE[size];
int i;
for(i = 0; i < size; i++)
{
temp = pBits[size - i];
}
for(int line = 0; line < m_Height; line++)
{
for(int row = 0; row < m_Width; row++)
{
temp2[line * m_Width + row] = temp[line * m_Width - row + m_Width];
}
}
for (int j = 0; j < m_Height; j++)
{
memcpy(&m_pBits[j*m_Width], &temp2[(m_Height - 1 - j) * m_Width], m_Width);
fwrite(&m_pBits[j*m_Width],m_Width,1,fp);
}
fclose(fp);
int scanline = SetDIBitsToDevice (hdc, 0, 0, m_Width, m_Height,0, 0, 0, m_Height,temp2, (LPBITMAPINFO) m_pBMI, DIB_RGB_COLORS);
delete [] temp;
delete [] temp2;
}
The code used to call the above functions is as follows:-
g_DibCtl->DibInit(devList.imgSize.width,devList.imgSize.height, filename);
g_DibCtl->SetBits(hdc,g_FPBuffer,devList.imgSize.width*devList.imgSize.height );
The problem that i am facing is : the bitmap is blank, if the bmp file is selected(e.g. in explorer), the preview is not available.If i try to open the bmp file in microsoft photo editor, an error : "Error Reading File" occurs. And if opened in Paint the bmp file is blank.
what is wrong?
Please help me and tell me the possible problem.
Thanks.