#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Crop image from clipboard


    I have a huge favor to ask this forum.
    I’m an economist and my language of choice is Excel – VBA. That is, I’m may need help implementing a code written in C/C++ or C#.

    I need a very simple standalone program that can do the following:
    - Before the program is run I’ve taken a screenshot, that is, there is an image in my clipboard.
    (1) Use the image in my clipboard.
    (2) Crop the image to my specifications.
    (3) Place the new cropped image in my clipboard.

    Explanation for (2): the specifications will change, so I need the source code, so I can change it myself.

    I need the program to run on Windows XP and up. I have “Microsoft Visual C# 2010 Express” and “Microsoft Visual C++ 2010 Express” installed on my computer.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Don't write code. Install imagemagick.

    The command line will be something like

    C:> convert clip:image -crop 20x40+10+4 clip:
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    I’m may need help implementing a code written in C/C++ or C#.
    IMHO, I'd write this application in C#. Image manipulation in C++ can be extremely challenging. If you do decide to use the C# approach, it may behoove you to post your request in the Net Development forum. Finally, post as much detail as possible about your app requirements. It will make life a lot easier for anyone trying to help you.

    Comments on this post

    • b49P23TIvg disagrees : My advice comes from Nobel Laureate Kenneth Wilson, Professor of Physics and Nuclear Studies, Director of the Cornell Theory Center (1963-1988), 1982 Prize in Physics. Don't write code if it's already available!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    C:> convert clip:image -crop 20x40+10+4 clip:
    If you read the Imagemagick documentation for the convert utility, it indicates that the clip parm is indicating that the clipping should be following the first path from the 8BIM Profile. The 8BIM profile is essentially an Adobe Photoshop graphics file format. I just can't understand how you can make the connection between a Adobe clip parameter and accessing the clipboard. Secondly, the "image" makes absolutely no sense to me. Is this an image file name? If so, how would convert utility extract this name from the clipboard heap? The reason being is that no name can be found on the heap. Only the metadata and pixel data can be found in the clipboard heap.

    I've pored over the Imagmagick documentation for a couple of hours and your suggestion is at best extremely questionable.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    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.

    You will first have to create two bitmap files named cropped.bmp and uncropped.bmp. Place both files in the same folder as the source code. Next, you'll have to compile from the command line as follows:

    rc.exe clip.rc
    cl.exe clip.cpp clip.res

    There are a total of three files. clip.cpp, clip.h and clip.rc

    To use the example, you will start at the top of the Clipboard menu and work your way down to the bottom of the Clipboard menu. It will take the original uncropped image and convert/display a cropped image. You will have to tweak the code to meet your specific needs.

    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) ;
    }
    Code:
    // clip.h
    #define IDM_SENDCLIP        1       /* menu item ID numbers */
    #define IDM_GETBMP          2
    #define IDM_SAVE            3
    #define IDM_SENDCROPPEDCLIP 4
    #define IDM_GETCROPPEDBMP   5
    #define IDM_QUIT            10
    
    #define S_PROGRAMCAPTION    1   /* string table ID numbers */
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    Code:
    // clip.rc
    #include "clip.h"
    
    TEMPBMP   		 BITMAP  uncropped.bmp
    TEMPCROPPEDPBMP   BITMAP  cropped.bmp
    
    MyMenu      MENU
    BEGIN
        POPUP   "&Clipboard"
        BEGIN
            MENUITEM "&Copy original bmp to clipboard",  IDM_SENDCLIP
            MENUITEM "Display original bmp to clipboard in window",  IDM_GETBMP
    	    MENUITEM "Save bitmap to cropped.bmp",   IDM_SAVE
    		 MENUITEM "Copy the cropped.bmp image to clipboard",   IDM_SENDCROPPEDCLIP
    		  MENUITEM "Display cropped bmp in window",   IDM_GETCROPPEDBMP
        END
        MENUITEM "&Quit",               IDM_QUIT 
    END
    
    STRINGTABLE
    BEGIN
        S_PROGRAMCAPTION    "Clipboard 2 Program"
    END
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    (Yes, I specified convert source and destination as clipboard: not clip:)

    I installed ImageMagick-6.8.0-4 onto a 64 bit computer running 32 bit Windows-NT system.

    Next I opened a cmd window and changed directory to that with the newly installed convert program.

    ********

    I next opened a google chrome to a website, button-3 clicked on an image, selected "copy image". That put the image into the clipboard.

    Next I commanded
    convert clipboard:myimage -crop 10x100+10+10 a.png
    Double clicking a.png from a directory browser showed the expected image.

    Note that
    convert clipboard:myimage -crop 10x100 a.png
    can create a whole lot of files by sectioning the image into 10x100 pieces.

    ********

    However, the original poster wanted to transfer from clipboard to clipboard. I commanded
    Code:
    convert clipboard:myimage -crop 10x100+10+10 clipboard:
    Next I opened gimp and created an image from clipboard. The result was exactly what I expected.

    Discussion: This wasn't straightforward. I first looked at My Computer properties, which said it's a 64 bit system so I installed a 64 bit version of ImageMagick. No go, so I went with the 32 bit version. I also had to refresh the picture in the clipboard twice, once because I stuffed the text name of the directory into the clipboard, replacing the picture, and once for unknown reason. These are marked with *******. I tried not "a lot" but "several" commands that failed. Given that I didn't report each command I might have put something else on the clipboard. The important point is that there are no asterisks within
    However, the original poster wanted to transfer from clipboard to clipboard. I commanded
    Code:
    convert clipboard:myimage -crop 10x100+10+10 clipboard:
    Next I opened gimp and created an image from clipboard. The result was exactly what I expected.
    Last edited by b49P23TIvg; November 10th, 2012 at 09:49 AM. Reason: disable smilies.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile


    Originally Posted by BobS0327
    The following fundamental code will illustrate the basic concepts of modifying an image in the clipboard.

    You will first have to create two bitmap files named cropped.bmp and uncropped.bmp. Place both files in the same folder as the source code. Next, you'll have to compile from the command line as follows:

    rc.exe clip.rc
    cl.exe clip.cpp clip.res

    There are a total of three files. clip.cpp, clip.h and clip.rc

    To use the example, you will start at the top of the Clipboard menu and work your way down to the bottom of the Clipboard menu. It will take the original uncropped image and convert/display a cropped image. You will have to tweak the code to meet your specific needs.

    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) ;
    }
    Code:
    // clip.h
    #define IDM_SENDCLIP        1       /* menu item ID numbers */
    #define IDM_GETBMP          2
    #define IDM_SAVE            3
    #define IDM_SENDCROPPEDCLIP 4
    #define IDM_GETCROPPEDBMP   5
    #define IDM_QUIT            10
    
    #define S_PROGRAMCAPTION    1   /* string table ID numbers */
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    Code:
    // clip.rc
    #include "clip.h"
    
    TEMPBMP   		 BITMAP  uncropped.bmp
    TEMPCROPPEDPBMP   BITMAP  cropped.bmp
    
    MyMenu      MENU
    BEGIN
        POPUP   "&Clipboard"
        BEGIN
            MENUITEM "&Copy original bmp to clipboard",  IDM_SENDCLIP
            MENUITEM "Display original bmp to clipboard in window",  IDM_GETBMP
    	    MENUITEM "Save bitmap to cropped.bmp",   IDM_SAVE
    		 MENUITEM "Copy the cropped.bmp image to clipboard",   IDM_SENDCROPPEDCLIP
    		  MENUITEM "Display cropped bmp in window",   IDM_GETCROPPEDBMP
        END
        MENUITEM "&Quit",               IDM_QUIT 
    END
    
    STRINGTABLE
    BEGIN
        S_PROGRAMCAPTION    "Clipboard 2 Program"
    END


    hello friends....as far as i have seen C programming (newbie),i have not seen this DWORD ,.rc file .res file .. what is all this i dont know at all..... if its some concept what is its name...i can google it...please explain....
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    I think you should not be obsessed over this. If the code is valid there will be macros or typedefs by the time the included header files are loaded. Do you have rc.exe and rl.exe ?
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I think you should not be obsessed over this. If the code is valid there will be macros or typedefs by the time the included header files are loaded. Do you have rc.exe and rl.exe ?
    I mean to say i havent used this in my life... its first time i am seeing it......

    i have used only one .c file always..... and newbie... so i want to know what is it exactly...
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    Originally Posted by swapy
    I mean to say i havent used this in my life... its first time i am seeing it......

    i have used only one .c file always..... and newbie... so i want to know what is it exactly...
    You must first download and install Visual Studio Express. This will create a Visual Studio command prompt for you. Open up this VS command prompt and execute RC.exe as indicated in the previous post. Then execute CL.exe as indicated in the previous post.

    You are building a Windows Win32 GUI app by using the command line tools which will be used to load and display your bitmaps.

    The RC.exe is a resource compiler. A description of this compiler can be found here. The resource file essentially provides the menu options which allow you to process the bitmap file in various stages.

    The CL.exe is a command line compiler that is used to integrate your resource file with the source file to create a binary end product.

    There is a more efficient means to building the application such as creating a Win32 project. But at this point, I'm trying to keep it as simple as possible. I would suggest that you build and run the app to see how it functions and then scavenge the code that you would need for your specific needs.

IMN logo majestic logo threadwatch logo seochat tools logo