I used a function of Daniel Garcia-Gil found in this forum but I make the bitmap in black
I modified the function found in Microsoft MSDN and running, but my knowledge does not allow me to save the file as JPG and BMP only as
He usado una function de Daniel Garcia-Gil encontrada en este foro pero me crea el bitmap en negro
He modificado la function encontrada en el MSDN de Microsoft y funciona correctamente, pero mis conocimientos no me permiten guardar el fichero como JPG y solo como BMP
#include "Fivewin.ch"
#define SRCCOPY 聽 聽 聽 聽 聽0x00CC0020
#define SM_CXSCREEN 聽 聽 聽0
#define SM_CYSCREEN 聽 聽 聽1
#define HALFTONE 聽4
FUNCTION MAIN()
聽 聽 LOCAL oDlg, oImg
聽 聽 DEFINE DIALOG oDlg;
聽 聽 聽 聽 聽 聽SIZE 800, 600
聽 聽 @ 0, 0 IMAGE oImg;
聽 聽 聽 聽 聽 聽FILE "WORD.JPG" OF oDlg
聽 聽 @ 15, 5 BUTTON "Draw";
聽 聽 聽 聽 聽 聽 ACTION ( DRAWIMG( oImg ) ) //, oImg:Refresh() )
聽 聽 聽 聽 聽 聽
聽 聽 @ 15, 25 BUTTON "Save FW";
聽 聽 聽 聽 聽 聽 聽ACTION SaveMiBmp( oDlg, oImg:nTop, oImg:nLeft, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽oImg:nWidth, oImg:nHeight, "IMAGENFW.BMP" ) //.JPG" )
聽 聽 聽 聽 聽 聽 //ACTION oImg:SaveImage( "IMGENFW.JPG", 2 )
聽 聽 @ 15, 40 BUTTON "Save C";
聽 聽 聽 聽 聽 聽 聽ACTION CaptureImage( oDlg:hWnd, oImg:nTop, oImg:nLeft, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 oImg:nWidth, oImg:nHeight, "IMAGENC.BMP" )
聽 聽 @ 15, 55 BUTTON "Exit" ACTION oDlg:End()
聽 聽 ACTIVATE DIALOG oDlg;
聽 聽 聽 聽 聽 聽 聽CENTER
聽 聽 RETURN NIL
STATIC FUNCTION DRAWIMG( oImg )
聽 聽 LOCAL hDC := oImg:GetDC()
聽 聽 Local uImg
聽 聽 LOCAL x, y
聽 聽 FOR y = 10 TO 50
聽 聽 聽 聽 FOR x = 10 TO 50
聽 聽 聽 聽 聽 聽 SETPIXEL( hDC, x, y, CLR_HRED )
聽 聽 聽 聽 NEXT
聽 聽 NEXT
聽 聽 oImg:ReleaseDC()
RETURN uImg
//-----------------------------------------------------------------------------
//http://forums.fivetechsupport.com/viewtopic.php?f=6&t=19403&p=137661&hilit=SaveToBmp#p102241
// SaveToBmp2 de Daniel Garcia-Gil con modificaciones
// Cristobal Navarro
//-----------------------------------------------------------------------------
Function SaveMiBmp( oWnd, nTop, nLeft, nWidth, nHeight, cBmpFile )
聽 聽local hParDC 聽 := oWnd:GetDC()
聽 聽local hBmp
聽 聽local hOldBmp
聽 聽local hDib
聽 聽local hDC 聽 聽 聽:= CreateCompatibleDC( hParDC )
聽 聽 //This is the best stretch mode
聽 聽 SetStretchBltMode(hParDC,HALFTONE)
聽 聽 //The source DC is the entire screen and the destination DC is the current window (HWND)
聽 聽 StretchBlt(hParDC,;
聽 聽 聽 聽 聽 聽 聽 聽0,0,;
聽 聽 聽 聽 聽 聽 聽 聽nWidth, nHeight,;
聽 聽 聽 聽 聽 聽 聽 聽hDC,;
聽 聽 聽 聽 聽 聽 聽 聽0,0,;
聽 聽 聽 聽 聽 聽 聽 聽GetSysMetrics( nWidth - nLeft ),; 聽//GetSystemMetrics (SM_CXSCREEN),
聽 聽 聽 聽 聽 聽 聽 聽GetSysMetrics( nHeight - nTop ),; 聽//GetSystemMetrics (SM_CYSCREEN),
聽 聽 聽 聽 聽 聽 聽 聽SRCCOPY)
聽 聽hBmp 聽 聽 聽:= CreateCompatibleBitmap( hDC, nWidth, nHeight )
聽 聽hOldBmp 聽 := SelectObject( hDC, hBmp )
聽 聽BitBlt( hDC, 0, 0, nWidth, nHeight, hParDC, 0, 0, SRCCOPY )
聽 聽SelectObject( hDC, hOldBmp )
聽 聽DeleteDC( hDC )
聽 聽hDib = DibFromBitmap( hBmp )
聽 聽DibWrite( cBmpFile, hDib )
聽 聽GloBalFree( hDib )
聽 聽DeleteObject( hBmp )
聽 聽oWnd:ReleaseDC() 聽
Return ( File( cBmpFile ) )
//-----------------------------------------------------------------------------
// Daniel Garcia - Gil 聽- 聽Original
//http://forums.fivetechsupport.com/viewtopic.php?f=6&t=19403&p=137661&hilit=SaveToBmp#p102241
//-----------------------------------------------------------------------------
Function SaveToBmp2( oWnd, cBmpFile )
聽 聽
聽 聽local hDeskDC := GetDC( GetDesktopWindow() )
聽 聽local hDC 聽:= CreateCompatibleDC( hDeskDC )
聽 聽local hOldBmp
聽 聽local hDib
聽 聽local hBmp
聽 聽local nWidth //:= GetSysMetrics( SM_CXSCREEN )
聽 聽local nHeight //:= GetSysMetrics( SM_CYSCREEN )
聽 聽
聽 聽 聽
聽 聽hBmp = CreateCompatibleBitmap( hDeskDC, nWidth, nHeight )
聽 聽hOldBmp = SelectObject( hDC, hBmp )
聽 聽
聽 聽// No // PrintWindow( oWnd:hWnd, hDC )
聽 聽
聽 聽BitBlt( hDC, 0, 0, nWidth, nHeight, hDeskDC, 0, 0, SRCCOPY )
聽 聽
聽 聽SelectObject( hDC, hOldBmp )
聽 聽DeleteDC( hDC )
聽 聽hDib = DibFromBitmap( hBmp )
聽 聽DibWrite( cBmpFile, hDib )
聽 聽GloBalFree( hDib )
聽 聽DeleteObject( hBmp )
return ( File( cBmpFile ) )
聽
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#pragma BEGINDUMP
//#define STRICT
#include "stdafx.h"
#include "windows.h"
#include "hbapi.h"
//-----------------------------------------------------------------------------
//http://msdn.microsoft.com/en-us/library/windows/desktop/dd183402(v=vs.85).aspx
// Modificada por Cristobal Navarro
//-----------------------------------------------------------------------------
int CaptureAnImage(HWND hWnd,
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽int nTop, int nLeft, 聽
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽int nW , int nH , LPCTSTR pszFichero )
{
聽 聽 HDC hdcScreen;
聽 聽 HDC hdcWindow;
聽 聽 HDC hdcMemDC = NULL;
聽 聽 HBITMAP hbmScreen = NULL;
聽 聽 BITMAP bmpScreen;
聽 聽 // Retrieve the handle to a display device context for the client
聽 聽 // area of the window.
聽 聽 hdcScreen = GetDC(NULL);
聽 聽 hdcWindow = GetDC(hWnd);
聽 聽 // Create a compatible DC which is used in a BitBlt from the window DC
聽 聽 hdcMemDC = CreateCompatibleDC(hdcWindow);
聽 聽 if(!hdcMemDC)
聽 聽 {
聽 聽 聽 聽 MessageBox(hWnd, TEXT("CreateCompatibleDC has failed"), TEXT("Failed"), MB_OK);
聽 聽 聽 聽 goto done;
聽 聽 }
聽 聽 // Get the client area for size calculation
聽 聽 RECT rcClient;
聽 聽 rcClient.top = nTop; //0;
聽 聽 聽 聽 rcClient.left = nLeft; //0;
聽 聽 聽 聽 rcClient.right = nW; //200;
聽 聽 聽 聽 rcClient.bottom = nH; //100;
聽 聽 //GetWindowRect(hWnd, &rcClient );
聽 聽 //GetClientRect(hWnd, &rcClient );
聽 聽 //This is the best stretch mode
聽 聽 SetStretchBltMode(hdcWindow,HALFTONE);
聽 聽 //The source DC is the entire screen and the destination DC is the current window (HWND)
聽 聽 if(!StretchBlt(hdcWindow,
聽 聽 聽 聽 聽 聽 聽 聽0,0,
聽 聽 聽 聽 聽 聽 聽 聽rcClient.right, rcClient.bottom,
聽 聽 聽 聽 聽 聽 聽 聽hdcScreen,
聽 聽 聽 聽 聽 聽 聽 聽0,0,
聽 聽 聽 聽 聽 聽 聽 聽GetSystemMetrics( rcClient.right - rcClient.left ), 聽//GetSystemMetrics (SM_CXSCREEN),
聽 聽 聽 聽 聽 聽 聽 聽GetSystemMetrics( rcClient.bottom - rcClient.top ), 聽//GetSystemMetrics (SM_CYSCREEN),
聽 聽 聽 聽 聽 聽 聽 聽SRCCOPY))
聽 聽 {
聽 聽 聽 聽 MessageBox(hWnd, TEXT("StretchBlt has failed"),TEXT("Failed"), MB_OK);
聽 聽 聽 聽 goto done;
聽 聽 }
聽 聽
聽 聽 // Create a compatible bitmap from the Window DC
聽 聽 hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top);
聽 聽
聽 聽 if(!hbmScreen)
聽 聽 {
聽 聽 聽 聽 MessageBox(hWnd, TEXT("CreateCompatibleBitmap Failed"),TEXT("Failed"), MB_OK);
聽 聽 聽 聽 goto done;
聽 聽 }
聽 聽 // Select the compatible bitmap into the compatible memory DC.
聽 聽 SelectObject(hdcMemDC,hbmScreen);
聽 聽
聽 聽 // Bit block transfer into our compatible memory DC.
聽 聽 if(!BitBlt(hdcMemDC,
聽 聽 聽 聽 聽 聽 聽 聽0,0,
聽 聽 聽 聽 聽 聽 聽 聽rcClient.right-rcClient.left,
聽 聽 聽 聽 聽 聽 聽 聽rcClient.bottom-rcClient.top,
聽 聽 聽 聽 聽 聽 聽 聽hdcWindow,
聽 聽 聽 聽 聽 聽 聽 聽0,0,
聽 聽 聽 聽 聽 聽 聽 聽SRCCOPY))
聽 聽 {
聽 聽 聽 聽 MessageBox(hWnd, TEXT("BitBlt has failed"), TEXT("Failed"), MB_OK);
聽 聽 聽 聽 goto done;
聽 聽 }
聽 聽 // Get the BITMAP from the HBITMAP
聽 聽 GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
聽 聽 聽
聽 聽 BITMAPFILEHEADER 聽 bmfHeader; 聽 聽
聽 聽 BITMAPINFOHEADER 聽 bi;
聽 聽 聽
聽 聽 bi.biSize = sizeof(BITMAPINFOHEADER); 聽 聽
聽 聽 bi.biWidth = bmpScreen.bmWidth; 聽 聽
聽 聽 bi.biHeight = bmpScreen.bmHeight; 聽
聽 聽 bi.biPlanes = 1; 聽 聽
聽 聽 bi.biBitCount = 32; 聽 聽
聽 聽 bi.biCompression = BI_RGB; 聽 聽
聽 聽 bi.biSizeImage = 0; 聽
聽 聽 bi.biXPelsPerMeter = 0; 聽 聽
聽 聽 bi.biYPelsPerMeter = 0; 聽 聽
聽 聽 bi.biClrUsed = 0; 聽 聽
聽 聽 bi.biClrImportant = 0;
聽 聽 DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
聽 聽 // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
聽 聽 // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
聽 聽 // have greater overhead than HeapAlloc.
聽 聽 HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
聽 聽 char *lpbitmap = (char *)GlobalLock(hDIB); 聽 聽
聽 聽 // Gets the "bits" from the bitmap and copies them into a buffer
聽 聽 // which is pointed to by lpbitmap.
聽 聽 GetDIBits(hdcWindow, hbmScreen, 0,
聽 聽 聽 聽 (UINT)bmpScreen.bmHeight,
聽 聽 聽 聽 lpbitmap,
聽 聽 聽 聽 (BITMAPINFO *)&bi, DIB_RGB_COLORS);
聽 聽 // A file is created, this is where we will save the screen capture.
聽 聽 HANDLE hFile = CreateFile( pszFichero , //TEXT("capture.bmp"),
聽 聽 聽 聽 GENERIC_WRITE,
聽 聽 聽 聽 0,
聽 聽 聽 聽 NULL,
聽 聽 聽 聽 CREATE_ALWAYS,
聽 聽 聽 聽 FILE_ATTRIBUTE_NORMAL, NULL); 聽
聽 聽
聽 聽 // Add the size of the headers to the size of the bitmap to get the total file size
聽 聽 DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
聽
聽 聽 //Offset to where the actual bitmap bits start.
聽 聽 bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
聽 聽
聽 聽 //Size of the file
聽 聽 bmfHeader.bfSize = dwSizeofDIB;
聽 聽
聽 聽 //bfType must always be BM for Bitmaps
聽 聽 bmfHeader.bfType = 0x4D42; //BM 聽
聽
聽 聽 DWORD dwBytesWritten = 0;
聽 聽 WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
聽 聽 WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
聽 聽 WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
聽 聽
聽 聽 //Unlock and Free the DIB from the heap
聽 聽 GlobalUnlock(hDIB); 聽 聽
聽 聽 GlobalFree(hDIB);
聽 聽 //Close the handle for the file that was created
聽 聽 CloseHandle(hFile);
聽 聽 聽 聽
聽 聽 //Clean up
done:
聽 聽 DeleteObject(hbmScreen);
聽 聽 DeleteObject(hdcMemDC);
聽 聽 ReleaseDC(NULL,hdcScreen);
聽 聽 ReleaseDC(hWnd,hdcWindow);
聽 聽 return 0;
}
HB_FUNC( CAPTUREIMAGE )
{
聽 CaptureAnImage( (HWND) hb_parnl( 1 ), hb_parnl( 2 ), hb_parnl( 3 ),
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽hb_parnl( 4 ), hb_parnl( 5 ), hb_parc( 6 ) ) ;
}
#pragma ENDDUMP