Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Saving image from picture control (http://www.programmingforums.org/showthread.php?t=15087)

darkies22 Jan 30th, 2008 10:51 AM

Saving image from picture control
 
Hi all,

I have a problem with my picture control. I draw lines inside this control on the basis of information I obtain from a COMPORT. The type of the picture control is "frame". After I finished analysing the data and completed drawing inside the control, I want to save what I have drawn in a non-compressed picture format (.bmp or .tif).

The way I draw the lines/rectangles inside the picture control is as followed:

DDX_Control(pDX, IDC_PICTURE, m_picCtrl); // obtaining handle to picture control

CClientDC dc(&m_picCtrl); // create client windows device context for drawing CDC memdc;
memdc.CreateCompatibleDC(&dc);
m_picCtrl.GetClientRect(&m_recClient);// get the coordinates of the client windows
dc.FilledSolidRect(&m_recClient, RGB(0,0,0)); // drawing rectangle/lines
etc etc.

I have googled a long time before I joined here and I really need help and tried things as CImage etc, but none worked for me.

darkies22 Jan 31st, 2008 6:53 AM

Re: Saving image from picture control
 
I think I found something. I have added the following code to my drawing function. The only problem is that it saves a bmp picture of the entire desktop (in short: it just takes a 'print screen'. I don't know what I am doing wrong.

[code=c++]

CClientDC dc(&m_picCtrl); // m_picCtrl is handle to picture control
CDC memdc;
memdc.CreateCompatibleDC(&dc);

HBITMAP hBmp = (HBITMAP)GetCurrentObject(dc, OBJ_BITMAP );


BITMAPINFO stBmpInfo;
stBmpInfo.bmiHeader.biSize = sizeof( stBmpInfo.bmiHeader );
stBmpInfo.bmiHeader.biBitCount = 0;
GetDIBits(hdc, hBmp, 0, 0, NULL, &stBmpInfo, DIB_RGB_COLORS );

ULONG iBmpInfoSize;
switch( stBmpInfo.bmiHeader.biBitCount )
{
case 24:
iBmpInfoSize = sizeof(BITMAPINFOHEADER);
break;
case 16:
case 32:
iBmpInfoSize = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3;
break;
default:
iBmpInfoSize = sizeof(BITMAPINFOHEADER)
+ sizeof(RGBQUAD)
* ( 1 << stBmpInfo.bmiHeader.biBitCount );
break;
}

PBITMAPINFO pstBmpInfo;
if( iBmpInfoSize != sizeof(BITMAPINFOHEADER) )
{
pstBmpInfo = (PBITMAPINFO)GlobalAlloc
( GMEM_FIXED | GMEM_ZEROINIT, iBmpInfoSize );
PBYTE pbtBmpInfoDest = (PBYTE)pstBmpInfo;
PBYTE pbtBmpInfoSrc = (PBYTE)&stBmpInfo;
ULONG iSizeTmp = sizeof( BITMAPINFOHEADER );

while( iSizeTmp-- )
{
*( ( pbtBmpInfoDest )++ ) = *( ( pbtBmpInfoSrc )++ );
}
}

CString strFileName = _T("height.bmp");


HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, 0 , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE , NULL );

BITMAPFILEHEADER stBmpFileHder;
stBmpFileHder.bfType = 0x4D42; // 'BM'
stBmpFileHder.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ iBmpInfoSize + pstBmpInfo->bmiHeader.biSizeImage;
stBmpFileHder.bfReserved1 = 0;
stBmpFileHder.bfReserved2 = 0;
stBmpFileHder.bfOffBits = sizeof(BITMAPFILEHEADER) + iBmpInfoSize;

DWORD dRet;
WriteFile(hFile, (LPCVOID)&stBmpFileHder, sizeof(BITMAPFILEHEADER), &dRet, NULL );

PBYTE pBits = (PBYTE)GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT, stBmpInfo.bmiHeader.biSizeImage );

HBITMAP hBmpOld;
HBITMAP hTmpBmp = CreateCompatibleBitmap(hdc, pstBmpInfo->bmiHeader.biWidth, pstBmpInfo->bmiHeader.biHeight );
hBmpOld = (HBITMAP)SelectObject( hdc, hTmpBmp );
GetDIBits( hdc, hBmp, 0, pstBmpInfo->bmiHeader.biHeight, (LPSTR)pBits, pstBmpInfo, DIB_RGB_COLORS ); // here is probably where the error lies!

WriteFile( hFile, (LPCVOID)pstBmpInfo, iBmpInfoSize, &dRet, NULL );

WriteFile( hFile, (LPCVOID)pBits, pstBmpInfo->bmiHeader.biSizeImage, &dRet, NULL );

SelectObject( /*h*/dc, hBmpOld );
DeleteObject( hTmpBmp );
CloseHandle( hFile );
GlobalFree( pstBmpInfo );
GlobalFree( pBits );
[\code]


All times are GMT -5. The time now is 3:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC