I’m may need help implementing a code written in C/C++ or C#
The following fundamental code will illustrate the basic concepts of modifying an image in the clipboard.
Code:
// clip.cpp
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#include <windows.h>
#include "clip.h"
int Screenshotal(HDC hdc, char *pszflname, DWORD startx, DWORD starty, DWORD width, DWORD height)
{
HDC memdc;
HANDLE hfl;
DWORD dwBytes, dwWidth, dwHeight, dwNumColors, dwBPP, ColorSize;
void *pBits;
HBITMAP hbmp;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBQUAD colors[256];
BITMAPINFO bmpinfo;
HGDIOBJ hret;
dwWidth = width;
dwHeight = height;
dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
if (dwBPP <= 8)
dwNumColors = 256;
else
dwNumColors = 0;
if (!(memdc = CreateCompatibleDC(hdc)))
return (0);
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = dwWidth;
bmpinfo.bmiHeader.biHeight = dwHeight;
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = (WORD)dwBPP;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 0;
bmpinfo.bmiHeader.biYPelsPerMeter = 0;
bmpinfo.bmiHeader.biClrUsed = dwNumColors;
bmpinfo.bmiHeader.biClrImportant = dwNumColors;
hbmp = CreateDIBSection(hdc, &bmpinfo, DIB_PAL_COLORS, &pBits, NULL, 0);
if (!hbmp)
{
DeleteDC(memdc);
return (0);
}
hret = SelectObject(memdc, hbmp);
if (!hret || (hret == HGDI_ERROR))
{
DeleteDC(memdc);
return (0);
}
if (!BitBlt(memdc, 0, 0, dwWidth, dwHeight, hdc, 0, 0, SRCCOPY))
{
DeleteDC(memdc);
return (0);
}
if (dwNumColors)
dwNumColors = GetDIBColorTable(memdc, 0, dwNumColors, colors);
fileheader.bfType = 0x4D42;
ColorSize = dwNumColors * sizeof(RGBQUAD);
fileheader.bfSize = ((dwWidth*dwHeight*dwBPP) >> 3) + ColorSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileheader.bfReserved1 = fileheader.bfReserved2 = 0;
fileheader.bfOffBits = ColorSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
infoheader.biSize = sizeof(BITMAPINFOHEADER);
infoheader.biWidth = dwWidth;
infoheader.biHeight = dwHeight;
infoheader.biPlanes = 1;
infoheader.biBitCount = (WORD)dwBPP;
infoheader.biCompression = BI_RGB;
infoheader.biSizeImage = infoheader.biClrImportant = 0;
infoheader.biXPelsPerMeter = infoheader.biYPelsPerMeter = 0;
infoheader.biClrUsed = dwNumColors;
hfl = CreateFile(pszflname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hfl == INVALID_HANDLE_VALUE)
{
DeleteObject(hbmp);
{
DeleteDC(memdc);
return (0);
}
}
WriteFile(hfl, &fileheader, sizeof(BITMAPFILEHEADER), &dwBytes, 0);
WriteFile(hfl, &infoheader, sizeof(BITMAPINFOHEADER), &dwBytes, 0);
if (!dwNumColors)
WriteFile(hfl, colors, ColorSize, &dwBytes, 0);
ColorSize = (dwWidth * dwHeight * dwBPP) >> 3;
WriteFile(hfl, pBits, ColorSize, &dwBytes, 0);
CloseHandle(hfl);
DeleteObject(hbmp);
DeleteDC(memdc);
return (1);
}
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow)
{
HWND hWnd ; /* the window's "handle" */
MSG msg ; /* a message structure */
WNDCLASS wndclass ; /* window class structure */
char cBuf [128] ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = "MyMenu" ;
wndclass.lpszClassName = "MyClass" ;
/* register the window class */
if (!RegisterClass (&wndclass))
return 0 ;
LoadString (hInstance, S_PROGRAMCAPTION, cBuf, sizeof (cBuf)) ;
hWnd = CreateWindow ("MyClass", cBuf, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hWnd, nCmdShow) ; /* display the window */
UpdateWindow (hWnd) ;
while (GetMessage (&msg, NULL, 0, 0)) /* message loop */
{
TranslateMessage (&msg) ; /* translate keyboard messages */
DispatchMessage (&msg) ; /* send message to WndProc() */
}
return (msg.wParam) ;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
WPARAM wParam, LPARAM lParam)
{
HDC hDC, hMemDC ;
HINSTANCE hInstance ;
HANDLE hGmem, hDigits, hClipMem ;
HBITMAP hBitmap ;
LPSTR lpStr, lpClip ;
int i ;
BITMAP bm ;
static WORD wClipFormat ;
char cBuf [128] ;
HDC hdcScreen;
HDC hdcWindow;
BITMAP bmpScreen;
HANDLE hFile;
DWORD dwSizeofDIB;
DWORD dwBytesWritten = 0;
HANDLE hDIB;
DWORD dwBmpSize ;
HDC hdcMemDC;
RECT rcClient;
HBITMAP hbmScreen;
char *lpbitmap = NULL;
switch (wMessage) /* process windows messages */
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_SENDCLIP:
/* load a bitmap from resources into memory */
/* resulting handle can be sent to clipboard */
hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
hBitmap = LoadBitmap (hInstance, "TEMPBMP") ;
if (OpenClipboard (hWnd))
{
EmptyClipboard () ;
SetClipboardData (CF_BITMAP, hBitmap) ;
CloseClipboard () ;
}
break ;
case IDM_SENDCROPPEDCLIP:
// Clear the screen for new bitmap
// InvalidateRect (hWnd, NULL, TRUE) ;
/* load a bitmap from resources into memory */
/* resulting handle can be sent to clipboard */
hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
hBitmap = LoadBitmap (hInstance, "TEMPCROPPEDPBMP") ;
if (OpenClipboard (hWnd))
{
EmptyClipboard () ;
SetClipboardData (CF_BITMAP, hBitmap) ;
CloseClipboard () ;
}
break;
case IDM_GETBMP: /* read & display bitmap from clipboard */
hDC = GetDC (hWnd) ;
if (OpenClipboard (hWnd))
{ /* get handle to clipboard data */
hClipMem = GetClipboardData (CF_BITMAP) ;
if (hClipMem) /* if there is CF_BITMAP data */
{
hMemDC = CreateCompatibleDC (hDC) ;
SelectObject (hMemDC, hClipMem) ;
GetObject (hClipMem, sizeof(BITMAP), (LPSTR)&bm) ;
BitBlt (hDC, 0, 0, bm.bmWidth, bm.bmHeight,
hMemDC, 0, 0, SRCCOPY) ;
DeleteDC (hMemDC) ;
}
CloseClipboard () ;
}
ReleaseDC (hWnd, hDC) ;
break ;
case IDM_GETCROPPEDBMP:
hDC = GetDC (hWnd) ;
if (OpenClipboard (hWnd))
{ /* get handle to clipboard data */
hClipMem = GetClipboardData (CF_BITMAP) ;
if (hClipMem) /* if there is CF_BITMAP data */
{
hMemDC = CreateCompatibleDC (hDC) ;
SelectObject (hMemDC, hClipMem) ;
GetObject (hClipMem, sizeof(BITMAP), (LPSTR)&bm) ;
BitBlt (hDC, 0, 0, bm.bmWidth, bm.bmHeight,
hMemDC, 0, 0, SRCCOPY) ;
DeleteDC (hMemDC) ;
}
CloseClipboard () ;
}
ReleaseDC (hWnd, hDC) ;
break ;
case IDM_SAVE:
hDC = GetDC (hWnd) ;
if (OpenClipboard (hWnd))
{ /* get handle to clipboard data */
hClipMem = GetClipboardData (CF_BITMAP) ;
if (hClipMem) /* if there is CF_BITMAP data */
{
hMemDC = CreateCompatibleDC (hDC) ;
SelectObject (hMemDC, hClipMem) ;
GetObject (hClipMem, sizeof(BITMAP), (LPSTR)&bm) ;
Screenshotal(hMemDC, "cropped.bmp", 100, 100,bm.bmWidth-100, bm.bmHeight-100);
DeleteDC (hMemDC) ;
}
CloseClipboard () ;
InvalidateRect (hWnd, NULL, TRUE) ;
}
ReleaseDC (hWnd, hDC) ;
break;
case IDM_QUIT: /* Quit menu item */
DestroyWindow (hWnd) ; /* destroy window, */
break ; /* terminating application */
}
break ;
case WM_DESTROY: /* stop application */
PostQuitMessage (0) ;
break ;
default: /* default windows message processing */
return (DefWindowProc (hWnd, wMessage, wParam, lParam)) ;
}
return (0) ;
}