Solved

How to copy an image file into clipboard C/C++?

Posted on 2016-07-19
1
219 Views
Last Modified: 2016-07-21
I need a C++/C code example that demonstrates how to copy a 24-bit bmp picture file on the computer/disk into the clipboard.
I am working with a Visual C++ Win32 console application on Windows 7.
OpenClipboard();
EmptyClipboard();
?
hMem = GlobalAlloc(GMEM_MOVEABLE, ?);
memcpy(GlobalLock(hMem), ?, ?);
GlobalUnlock(hMem);
SetClipboardData(CF_DIB, hMem);
CloseClipboard();

Open in new window

Thanks to anyone that helps.
0
Comment
Question by:Member_2_7966362
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 41723559
the code is like the following for a 24-bit bitmapfile (not compressed).

#include <windows.h>
#include <sys/stat.h>
#include <fstream>
#include <errno.h>

int BitmapFileToClipBoard(HWND hwnd, const char * szBitmapFilePath)
{
    struct stat fs = { 0 };
    if (!stat(szBitmapFilePath, &fs) == 0)
        return errno;   // error file does not exist
    std::ifstream ifs(szBitmapFilePath, std::ios::binary | std:ios::in);
    unsigned char * buf = new unsigned char[fs.st_size];
    if (!ifs.read((char*)buf, fs.st_size)
    {
        int err = errno;
        ifs.close();
        return err;
    }
    ifs.close();
    BITMAPINFO * pDib = (BITMAPINFO *)buf;
 
    HDC hdc = CreateCompatibleDC(NULL);
    HBITMAP hBitmap = CreateDIBitmap(hdc, 
         &(pDib->bmiHeader),
         CBM_INIT,
         pDib->bmiColors,
         pDib,
         DIB_RGB_COLORS);
     if (OpenClipBoard(hwnd))
     {
          SetClipboardData(CF_BITMAP, hBitmap); 
          CloseClipboard();
          DeleteDC(hdc);
          return 0;
    }
    return -1;
}

Open in new window


the code is not tested and extracted from much bigger code such that i may have forgotten some necessary things. but you may got the idea from the above and use it as a starter.

Sara
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question