Solved

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

Posted on 2016-07-19
  • C++
  • Windows 7
  • Microsoft Development
  • Programming
  • Images and Photos
  • +1
1
196 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
1 Comment
 
LVL 33

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

839 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