Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2008, 9:51 AM   #1
darkies22
Newbie
 
Join Date: Jan 2008
Posts: 2
Rep Power: 0 darkies22 is on a distinguished road
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 is offline   Reply With Quote
Old Jan 31st, 2008, 5:53 AM   #2
darkies22
Newbie
 
Join Date: Jan 2008
Posts: 2
Rep Power: 0 darkies22 is on a distinguished road
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]
darkies22 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Downloading an external image and saving it to disk Jessehk PHP 6 Nov 8th, 2007 8:11 AM
manipulating pixels of images and saving them on a file diamondustice Visual Basic 0 Feb 21st, 2006 9:00 PM
adding an image control at run time diamondustice Visual Basic 4 Feb 19th, 2006 9:12 PM
Picture Box and Image box? java_roshan Visual Basic 1 Oct 25th, 2005 9:57 AM
Checking source codes of image, audio and video files on_auc C++ 3 Feb 21st, 2005 8:36 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:13 AM.

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