Hi,
I need to modify a 16 color bmp and then save it as a seperate file. I loaded a 16 color 48x48 bmp file on DC using the LoadImage() and BitBlt() API. Using GDI functions, I change the display on the DC, say draw a line on the DC, on top of the image displayed. Now I need to capture the current DC ( bmp image + the changes,i.e., the line) and create a new BMP file.
For 256 color and 16 color BMP file, the new BMP created is a plain white imgage, if I load the image with 'LR_CREATEDIBSECTION' flag in LoadImage API. else I get am able to create the file, but the image size is different. For the 24 bpp the application is working fine. Where am i doing wrong. I am using the 'CreateCompatibleBitmap()' to create the bitmap from DC. Is there any other way?.
<br />
HBITMAP hBMP;<br />
RECT r;<br />
GetClientRect(GetDlgItem(hWnd,IDC_STATIC_1),&r);<br />
HDC memDC = CreateCompatibleDC ( pDC );<br />
hBMP = (HBITMAP)LoadImage(hResDll ,filename,IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);<br />
SelectObject ( memDC, hBMP );<br />
BitBlt(pDC,r.left ,r.top ,imgWidth,imgHeight ,memDC,0,0,SRCCOPY);<br />
DeleteObject(hBMP);
..............................
To capture the modified BMP from DC
.............................
<br />
RECT rc;<br />
GetClientRect(GetDlgItem(hWndDlg,IDC_STATIC_1),&rc);<br />
HDC hScreenDC = GetWindowDC(GetDlgItem(hWndDlg,IDC_STATIC_1)); <br />
HDC hmemDC = CreateCompatibleDC(hScreenDC); <br />
int ScreenWidth = rc.right;<br />
int ScreenHeight = rc.bottom;<br />
HBITMAP hmemBM = CreateCompatibleBitmap(hScreenDC, ScreenWidth ,ScreenHeight );<br />
SelectObject(hmemDC, hmemBM);<br />
bufLen= filesize;<br />
HGLOBAL hpxldata = GlobalAlloc(GMEM_FIXED ,imgWidth * imgHeight * 3 ); <br />
void * lpvpxldata = GlobalLock(hpxldata);<br />
<br />
BITMAPINFO bmInfo;<br />
bmInfo.bmiHeader.biSize = 40;<br />
bmInfo.bmiHeader.biWidth = imgWidth;<br />
bmInfo.bmiHeader.biHeight = imgHeight;<br />
bmInfo.bmiHeader.biPlanes = 1;<br />
bmInfo.bmiHeader.biBitCount = 24;<br />
bmInfo.bmiHeader.biCompression = 0;<br />
bmInfo.bmiHeader.biSizeImage = 0;<br />
bmInfo.bmiHeader.biXPelsPerMeter = 0;<br />
bmInfo.bmiHeader.biYPelsPerMeter = 0;<br />
bmInfo.bmiHeader.biClrUsed = 0;<br />
bmInfo.bmiHeader.biClrImportant = 0;<br />
BITMAPFILEHEADER bmFileHeader;<br />
bmFileHeader.bfType = 19778;<br />
bmFileHeader.bfSize = filesize; <br />
bmFileHeader.bfReserved1 = 0;<br />
bmFileHeader.bfReserved2 = 0;<br />
bmFileHeader.bfOffBits = 54;<br />
StretchBlt(hmemDC,0 ,0 ,imgWidth ,imgHeight,hScreenDC,0,0,ScreenWidth,ScreenHeight ,SRCCOPY);<br />
int nret = GetDIBits(hmemDC, hmemBM, 0, imgHeight, lpvpxldata, &bmInfo, DIB_RGB_COLORS);<br />
<br />
<br />
ImgBuffer = (void*)malloc(bufLen); <br />
memset(ImgBuffer, 0, bufLen); <br />
memcpy((char*)ImgBuffer, (char*)&bmFileHeader,14);<br />
memcpy((char*)ImgBuffer + 14, (char*)&bmInfo, 40);<br />
memcpy((char*)ImgBuffer+54, (char*)lpvpxldata,bufLen - 54); <br />
FILE *fn = fopen("e:\\vinaya\\testimg.bmp","w"); <br />
fn = fopen("e:\\NewImg.bmp","w");<br />
fwrite((char*) ImgBuffer ,sizeof(char), filesize, fn);<br />
fclose(fn);<br />
fn = NULL;<br />
<br />
int i = GlobalUnlock(hpxldata);<br />
DWORD size = GlobalSize(hpxldata);<br />
HGLOBAL h = GlobalFree(hpxldata);<br />
DeleteObject(hmemBM);<br />
DeleteDC(hmemDC);<br />
ReleaseDC(0,hScreenDC);<br />
return ;
The new image need to be same in file size, resolution, bpp except the modification in image data. Kindly help.
Thanks
~Vini
|